2015-04-20 20 views
0

我正在使用Maven項目,我有兩個項目ProjectAProjectB。我ProjectA是一個Maven庫,其POM看起來是這樣的:如何在Maven中使用ProjectB中ProjectA的依賴關係?

項目A POM:

<?xml version="1.0" encoding="UTF-8"?> 
<project 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    <modelVersion>4.0.0</modelVersion> 

    <parent> 
     <groupId>com.texture.partial</groupId> 
     <artifactId>PartialPlatform</artifactId> 
     <version>2.1.5-RELEASE</version> 
    </parent> 

    <groupId>com.texture.transform.golden</groupId> 
    <artifactId>SampleClientProjectA</artifactId> 
    <version>1.0.4</version> 

    <dependencies> 
     <dependency> 
      <groupId>com.google.code.gson</groupId> 
      <artifactId>gson</artifactId> 
      <version>2.2.4</version> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.partial.core</groupId> 
      <artifactId>PartialKernel</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.webres</groupId> 
      <artifactId>WebResPartial</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>javax.servlet-api</artifactId> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>jsp-api</artifactId> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.kernel</groupId> 
      <artifactId>TextureServer</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.kernel</groupId> 
      <artifactId>Kernel</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.v3jars.Houston</groupId> 
      <artifactId>KerlDEL</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.kernel</groupId> 
      <artifactId>pKerl</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.kernel</groupId> 
      <artifactId>pKerlCore</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-beans</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>javax.servlet-api</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-test</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-asm</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-expression</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aop</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>aopalliance</groupId> 
      <artifactId>aopalliance</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.servicemix.bundles</groupId> 
      <artifactId>org.apache.servicemix.bundles.cglib</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.partial.core</groupId> 
      <artifactId>ConfigWeb</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.partial.core</groupId> 
      <artifactId>PartialWeb</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.googlecode.jmockit</groupId> 
      <artifactId>jmockit</artifactId> 
      <version>1.7</version> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <configuration> 
        <argLine>-javaagent:"${settings.localRepository}"/com/googlecode/jmockit/jmockit/1.7/jmockit-1.7.jar</argLine> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>cobertura-maven-plugin</artifactId> 
       <configuration> 
        <instrumentation> 
         <excludes> 
          <exclude>**/test/**/*.class</exclude> 
         </excludes> 
        </instrumentation> 
        <formats> 
         <format>xml</format> 
         <format>html</format> 
        </formats> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

在我上面的POM,PartialKernel帶來各種Spring框架依賴的舊版本一樣spring-corespring-web。它帶來3.2.8.RELEASE版本,我想使用這兩個彈簧框架的最新版本,它是4.1.6.RELEASE

<dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>4.1.6.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>4.1.6.RELEASE</version> 
    </dependency> 

我需要使用最新版本,因爲有些類只在最新版本中有。所以我在pom.xml中添加了這兩個庫的新版本,就像它在我的ProjectA中一樣。我有一個靜態的無效主代碼,它將測試ProjectA功能,並且沒有任何問題,工作正常。

現在我有ProjectB它也有上面的pom,因爲它沒有最新版本的彈簧依賴。在我的ProjectB POM中,我依賴ProjectA和相同的代碼,它將測試ProjectA的功能,但是每當我在ProjectB中運行相同的類時,我總是會收到此錯誤。

Exception in thread "main" java.lang.NoClassDefFoundError: org.springframework.util.concurrent.ListenableFutureCallback 
Caused by: java.lang.ClassNotFoundException: org.springframework.util.concurrent.ListenableFutureCallback 

裏面我是用所有最新的春天相關的代碼是在ProjectA中,我已經有最新版本的春天。我有的樣本測試代碼只是調用ProjectA類來完成測試。

項目B POM:

<?xml version="1.0" encoding="UTF-8"?> 
<project 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    <modelVersion>4.0.0</modelVersion> 

    <parent> 
     <groupId>com.texture.partial</groupId> 
     <artifactId>PartialPlatform</artifactId> 
     <version>2.1.5-RELEASE</version> 
    </parent> 

    <groupId>com.texture.transform.golden</groupId> 
    <artifactId>SampleTestClientProjectB</artifactId> 
    <version>1.0.0</version> 

    <dependencies> 
     <dependency> 
      <groupId>com.google.code.gson</groupId> 
      <artifactId>gson</artifactId> 
      <version>2.2.4</version> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.partial.core</groupId> 
      <artifactId>PartialKernel</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.webres</groupId> 
      <artifactId>WebResPartial</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>javax.servlet-api</artifactId> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>jsp-api</artifactId> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.kernel</groupId> 
      <artifactId>TextureServer</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.kernel</groupId> 
      <artifactId>Kernel</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.v3jars.Houston</groupId> 
      <artifactId>KerlDEL</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.kernel</groupId> 
      <artifactId>pKerl</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.kernel</groupId> 
      <artifactId>pKerlCore</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-beans</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>javax.servlet-api</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-test</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-asm</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-expression</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aop</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>aopalliance</groupId> 
      <artifactId>aopalliance</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.servicemix.bundles</groupId> 
      <artifactId>org.apache.servicemix.bundles.cglib</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.partial.core</groupId> 
      <artifactId>ConfigWeb</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.texture.partial.core</groupId> 
      <artifactId>PartialWeb</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.googlecode.jmockit</groupId> 
      <artifactId>jmockit</artifactId> 
      <version>1.7</version> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>com.texture.transform.golden</groupId> 
      <artifactId>SampleClientProjectA</artifactId> 
      <version>1.0.4</version>  
     </dependency> 
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <configuration> 
        <argLine>-javaagent:"${settings.localRepository}"/com/googlecode/jmockit/jmockit/1.7/jmockit-1.7.jar</argLine> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>cobertura-maven-plugin</artifactId> 
       <configuration> 
        <instrumentation> 
         <excludes> 
          <exclude>**/test/**/*.class</exclude> 
         </excludes> 
        </instrumentation> 
        <formats> 
         <format>xml</format> 
         <format>html</format> 
        </formats> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

但是,當我在我的項目B添加最新版本的Spring依賴的,我的測試代碼開始工作的罰款。這就是我不想要的。有沒有辦法,我的ProjectB自動開始使用我在ProjectA中使用的任何依賴關係?如果他們想改變它,那麼他們可以在ProjectB中的代碼中覆蓋它。

回答

1

你可以嘗試從pom xml中排除依賴關係。這裏是例子。

<dependency> 
    <groupId>com.texture.partial.core</groupId> 
    <artifactId>PartialKernel</artifactId> 
    <exclusions> 
     <exclusion> <!-- declare the exclusion here --> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
     </exclusion> 
     <exclusion> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
     </exclusion> 
    </exclusions> 
</dependency> 
+0

感謝您的幫助。我已經嘗試過,但沒有幫助。當我運行它時,我仍然在ProjectB代碼中得到相同的異常。 – john

+0

對不起,你打算直接從本身運行項目B還是試圖運行項目A? –

+0

我有一個ProjectB中的主類,我直接運行它,這給了我例外。和我在ProjectA中有相同的主類,工作正常。所以ProjectB的主要類將運行ProjectA。 – john