java - 如何在maven中使用Project in Project的依赖?

标签 java spring maven dependency-management

我正在使用 Maven 项目,我有两个项目,ProjectAProjectB 。我的ProjectA是一个maven库,其pom如下所示:

ProjectA 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 Framework 依赖项的旧版本,例如 spring-core , spring-web 。它带来了3.2.8.RELEASE版本,我想使用这两个spring框架的最新版本,即 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 中一样。我有一个 static void main 代码,将测试 ProjectA功能并且工作正常,没有任何问题。

现在我有ProjectB它也具有与上面相同的 pom,因为它没有最新版本的 spring 依赖项。在我的ProjectB pom,我依赖 ProjectA和相同的代码将测试 ProjectA 的功能但每当我在 ProjectB 中运行相同的类(class)时我总是收到此错误。

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

我使用的所有最新的 spring 相关代码都在 ProjectA 中其中我已经有最新版本的 spring 了。我拥有的示例测试代码只是调用 ProjectA类就是测试它的全部内容。

ProjectB 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>

但是当我在 ProjectB 中添加最新版本的 spring 依赖项时,我的测试代码开始正常工作。这就是我不想要的。有什么方法可以让我的 ProjectB 自动开始使用我在 ProjectA 中使用的任何依赖项?如果他们想要更改它,那么他们可以在 ProjectB 的代码中覆盖它。

最佳答案

您可以尝试从 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>

关于java - 如何在maven中使用Project in Project的依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29738773/

相关文章:

java.util.zip : putNextEntry

java - spring jdbctemplate和Hibernate的区别

java - 为什么使用框架的功能被认为是错误的

maven - GlassFish 3 + Maven + 远程部署

maven - 可以将 SonarQube 分析器从 Maven 切换到 Sonar-Runner 吗?

c# - 在不使用 StringBuffer.Append 或 StringBuilder.Append 的情况下在 Java/C# 中追加字符串

java - 为什么 Eclipse/JSP 会解析我的 JavaScript?

java - 迷宫求解器 ArrayIndexOutOfBoundsException

spring - 将 spring boot war 部署到 tomcat 中

java - 无法自省(introspection)类 [org.keycloak.adapters.springboot.KeycloakAutoConfiguration]