2016-06-09 55 views
0

我已經下載了兩個項目「chocolatestore-model」「springchocolatestore」用於學習目的。 「巧克力店模型」中的「springchocolatestore」依賴者,即它使用「巧克力店模型」中的服務。Spring-MVC的Maven相關性問題

在「springchocolatestore」的maven-Dependencies文件夾中,可以在裏面找到「chocolatestore-model」。現在我已經創建了一個新項目,並且還想使用「巧克力店模型」,以便我可以使用它的服務。我發現如何在我的項目的maven-dependencies中找到它很困難。

因此,我決定將「springchocolatestore」的相同pom.xml複製並粘貼到我的新項目中。保存文件後,「我的新項目的Maven依賴關係中不會出現」chocolatestore-model「。

當我第一次複製了前兩個proojects時,教程要求右鍵單擊「巧克力庫模型」並選擇Run-As-> maven-install,我不知道是否導致它自動安裝到「springchocolatestore」maven-dependencies文件夾或不。

我是maven的新手,所以很難理解POM文件中的所有內容。這些都是行家文件::

「springchocolatestore的pom.xml」 ::

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.packtpub</groupId> 
    <artifactId>springchocolatestore</artifactId> 
    <name>springchocolatestore</name> 
    <packaging>war</packaging> 
    <version>1.0.0-BUILD-SNAPSHOT</version> 
    <properties> 
     <java-version>1.7</java-version> 
     <org.springframework-version>4.0.2.RELEASE</org.springframework-version> 
     <org.aspectj-version>1.6.10</org.aspectj-version> 
     <org.slf4j-version>1.6.6</org.slf4j-version> 
    </properties> 
    <dependencyManagement> 
     <dependencies> 
      <dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-framework-bom</artifactId> 
       <version>4.0.2.RELEASE</version> 
       <type>pom</type> 
       <scope>import</scope> 
      </dependency> 
     </dependencies> 
    </dependencyManagement> 
    <dependencies> 
     <!-- Spring --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <exclusions> 
       <!-- Exclude Commons Logging in favor of SLF4j --> 
       <exclusion> 
        <groupId>commons-logging</groupId> 
        <artifactId>commons-logging</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aspects</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context-support</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-oxm</artifactId> 
     </dependency> 

     <!-- Spring HATEOAS --> 
     <dependency> 
      <groupId>org.springframework.hateoas</groupId> 
      <artifactId>spring-hateoas</artifactId> 
      <version>0.16.0.RELEASE</version> 
     </dependency> 

     <!-- Logging --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>${org.slf4j-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>jcl-over-slf4j</artifactId> 
      <version>${org.slf4j-version}</version> 
      <scope>runtime</scope> 
     </dependency> 


     <!-- @Inject --> 
     <dependency> 
      <groupId>javax.inject</groupId> 
      <artifactId>javax.inject</artifactId> 
      <version>1</version> 
     </dependency> 

     <!-- Servlet --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>jsp-api</artifactId> 
      <version>2.1</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 

     <!-- Freemarker --> 
     <dependency> 
      <groupId>org.freemarker</groupId> 
      <artifactId>freemarker</artifactId> 
      <version>2.3.20</version> 
     </dependency> 

     <!-- Apache Tiles --> 
     <dependency> 
      <groupId>org.apache.tiles</groupId> 
      <artifactId>tiles-extras</artifactId> 
      <version>3.0.0</version> 
     </dependency> 

     <!-- Test --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.7</version> 
      <scope>test</scope> 
     </dependency> 

     <!-- Chocolate store Model dependency --> 
     <dependency> 
      <groupId>com.packtpub.springmvc.chocolatestore.model</groupId> 
      <artifactId>chocolatestore-model</artifactId> 
      <version>1.2</version> 
     </dependency> 
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.tomcat.maven</groupId> 
       <artifactId>tomcat7-maven-plugin</artifactId> 
       <version>2.0</version> 
      </plugin> 
      <plugin> 
       <artifactId>maven-eclipse-plugin</artifactId> 
       <version>2.9</version> 
       <configuration> 
        <additionalProjectnatures> 
         <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> 
        </additionalProjectnatures> 
        <additionalBuildcommands> 
         <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> 
        </additionalBuildcommands> 
        <downloadSources>true</downloadSources> 
        <downloadJavadocs>true</downloadJavadocs> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.5.1</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
        <compilerArgument>-Xlint:all</compilerArgument> 
        <showWarnings>true</showWarnings> 
        <showDeprecation>true</showDeprecation> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>exec-maven-plugin</artifactId> 
       <version>1.2.1</version> 
       <configuration> 
        <mainClass>org.test.int1.Main</mainClass> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

「chocolatestore模型的pom.xml」 ::

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.packtpub.springmvc.chocolatestore.model</groupId> 
    <artifactId>chocolatestore-model</artifactId> 
    <packaging>jar</packaging> 
    <version>1.2</version> 
    <name>chocolatestore-model</name> 

    <properties> 

     <!-- Generic properties --> 
     <java.version>1.7</java.version> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 

     <!-- Spring --> 
     <spring-framework.version>4.0.1.RELEASE</spring-framework.version> 

     <!-- Hibernate/JPA --> 
     <hibernate.version>4.2.2.Final</hibernate.version> 

     <!-- Logging --> 
     <logback.version>1.0.13</logback.version> 
     <slf4j.version>1.7.5</slf4j.version> 

     <!-- Test --> 
     <junit.version>4.11</junit.version> 

    </properties> 

    <dependencies> 
     <!-- Spring and Transactions --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>${spring-framework.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-tx</artifactId> 
      <version>${spring-framework.version}</version> 
     </dependency> 

     <!-- Logging with SLF4J & LogBack --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>${slf4j.version}</version> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>ch.qos.logback</groupId> 
      <artifactId>logback-classic</artifactId> 
      <version>${logback.version}</version> 
      <scope>runtime</scope> 
     </dependency> 

     <!-- Hibernate --> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>${hibernate.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>${hibernate.version}</version> 
      <exclusions> 
       <exclusion> 
        <groupId>cglib</groupId> 
        <artifactId>cglib</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>dom4j</groupId> 
        <artifactId>dom4j</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate.javax.persistence</groupId> 
      <artifactId>hibernate-jpa-2.0-api</artifactId> 
      <version>1.0.1.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-collections</groupId> 
      <artifactId>commons-collections</artifactId> 
      <version>3.2.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
      <version>4.3.1.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.validation</groupId> 
      <artifactId>validation-api</artifactId> 
      <version>1.0.0.GA</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.transaction</groupId> 
      <artifactId>jta</artifactId> 
      <version>1.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-jdbc</artifactId> 
      <version>${spring-framework.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>${spring-framework.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-pool</groupId> 
      <artifactId>commons-pool</artifactId> 
      <version>1.5.6</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-dbcp</groupId> 
      <artifactId>commons-dbcp</artifactId> 
      <version>1.4</version> 
      <exclusions> 
       <exclusion> 
        <groupId>commons-logging</groupId> 
        <artifactId>commons-logging</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>xml-apis</groupId> 
        <artifactId>xml-apis</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 


     <!-- Database --> 
     <dependency> 
      <groupId>org.hsqldb</groupId> 
      <artifactId>hsqldb</artifactId> 
      <version>2.2.9</version> 
     </dependency> 


     <!-- Test Artifacts --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-test</artifactId> 
      <version>${spring-framework.version}</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>${junit.version}</version> 
      <scope>test</scope> 
     </dependency> 

     <!-- Commons --> 
     <dependency> 
      <groupId>org.apache.commons</groupId> 
      <artifactId>commons-lang3</artifactId> 
      <version>3.1</version> 
     </dependency> 

     <!-- Jackson --> 
     <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-mapper-asl</artifactId> 
      <version>1.9.13</version> 
     </dependency> 


    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.5.1</version> 
       <configuration> 
        <source>${java.version}</source> 
        <target>${java.version}</target> 
        <encoding>${project.build.sourceEncoding}</encoding> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-resources-plugin</artifactId> 
       <version>2.6</version> 
       <configuration> 
        <encoding>${project.build.sourceEncoding}</encoding> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-assembly-plugin</artifactId> 
       <version>2.3</version> 
       <configuration> 
        <descriptorRefs> 
         <descriptorRef>jar-with-dependencies</descriptorRef> 
        </descriptorRefs> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

我的新項目POM.XML與「springchocolatestore」的一樣,因爲我也想使用「巧克力店模型」的服務。只有這部分變化::

<modelVersion>4.0.0</modelVersion> 
<groupId>com.packtpub</groupId> 
<artifactId>springmvc</artifactId> 
<name>FirstSpringMVCProject</name> 
<packaging>war</packaging> 
<version>1.0.0-BUILD-SNAPSHOT</version> 

我希望有人能幫我在我的新項目的maven dependencies中加入「chocolatestore-model」。 感謝您的幫助

+0

夥計,這絕對容易!看看:https://maven.apache.org/guides/index.html真的很好的文檔!和非常好的教程。只要閱讀它,就會很好! – Ivan

回答

0
  • MVN安裝並建立我們所說的神器(你可以重用)和神器可到本地Maven倉庫。如果您沒有更改Maven配置,那麼在執行「安裝」之後,工件(您創建的組件)位於〜/ m2/repository(與主目錄相關)中。您當然可以檢查您製作的2個工件是否位於〜/ m2/repository/com/packtpub/springchocolatestore和〜/ m2/repository/com/packtpub/springmvc/chocolatestore/model/chocolatestore-model中。最新制作的神器出現在〜/ m2/com/packtpub/springmvc中。正如你可以審查你是不是很一致在你的工件的命名。您在groupidartifactid中使用springmvc,這不是一個好習慣。
  • 要重複使用工件,這非常容易。將artifactid引用到您的pom的部分依賴關係中。 你已經做到了在springchocolatestore的POM:
    ... <!-- Chocolate store Model dependency --> <dependency> <groupId>com.packtpub.springmvc.chocolatestore.model</groupId> <artifactId>chocolatestore-model</artifactId> <version>1.2</version> </dependency> </dependencies> 如果要使用相同的依賴在做用SpringMVC的POM相同。 如果您再次使用巧克力店模式,則意味着它在哪裏使用,您可以'好',使用。如果你想改變代碼,你必須重建巧克力店模型。它不在您使用它的地方,您可以修改它。你改變了,你重建(安裝)然後使用。
  • 我重複檢查是否可以構建(本地)artifact id springmvc,cd到springmvc,然後鍵入 clean install(命令行)。在開始的時候,與Maven相關的困難之處在於你創建了一個只有幾行代碼的pom.xml,然後你可以很容易地在本地構建軟件(如果你按照慣例做)。它被稱爲配置上的約定。


希望它有一點幫助。