简体   繁体   English

Maven:将相同工件的旧版本添加为依赖项

[英]Maven: Add older version of the same artifact as dependency

Is it possible to add older version of the same artifact as dependency? 是否可以将相同工件的较旧版本添加为依赖项? Here is an example: 这是一个例子:

<groupId>edu.illinois.cs.cogcomp</groupId>
<artifactId>illinois-pos</artifactId>
<version>2.0.3</version>

<dependencies>
    <dependency>
        <groupId>edu.illinois.cs.cogcomp</groupId>
        <artifactId>illinois-pos</artifactId>
        <version>2.0.2</version>
        <classifier>model</classifier>
    </dependency>
</dependencies>

If not, is there any way to make it work? 如果没有,有什么办法可以使它起作用?

Based on your comments (and further clarification and needs), I would suggest to have a Maven multi-module project structured as following: 根据您的意见(以及进一步的澄清和需求),我建议构建一个Maven 多模块项目,其结构如下:

  • A module providing the code using the latest dependency, which would not provide any test 使用最新依赖项提供代码的模块,该模块将不提供任何测试
  • A module providing only test cases and using the old dependency and as such running the tests on the concerned code by using another dependency. 一个仅提供测试用例并使用旧依赖性的模块,因此,通过使用另一个依赖性对相关代码运行测试。 To make it work, the test module must have a dependency on the code module and explicitly re-declare the dependency version you effectively want for the tests. 为了使其正常工作,测试模块必须对代码模块具有依赖性,并显式重新声明您实际上想要进行测试的依赖性版本。

As such, you would have a an aggregator pom (empty project, no sources, no tests) having packaging pom and defining two modules (ie, code-module, test-module). 这样,您将拥有一个聚合pom(空项目,无源,无测试),该pom具有packaging pom并定义了两个模块(即,代码模块,测试模块)。 You would keep on working on the code-module and then provide your test cases on the test-module, where you can also override and re-define any required dependency. 您将继续在代码模块上工作,然后在测试模块上提供测试用例,在这里您还可以覆盖并重新定义任何必需的依赖项。

As such, as part of the same build (on the aggregator project) you could build the two modules as you need. 这样,作为同一构建的一部分(在聚合器项目上),您可以根据需要构建两个模块。

Alternatively, keeping a single layer module, you could define a profile which would re-define the required dependency (with a different version) and run your test cases. 或者,保留一个单层模块,您可以定义一个配置文件 ,该配置文件将重新定义所需的依赖关系(具有不同的版本)并运行测试用例。 That would also mean that: 这也意味着:

  • As part of the default build, you will need to skip tests, configuring the Maven Surefire Plugin to skip tests via a property (ie <skip>${skip.tests}</skip> ) which would have as default value true 作为默认构建的一部分,您将需要跳过测试,将Maven Surefire插件配置为通过默认值为true的属性(即<skip>${skip.tests}</skip>跳过测试
  • You would then need to configure your profile to change the value of the skip.tests property to false and re-define the dependency 然后,您需要配置您的配置文件,以将skip.tests属性的值更改为false并重新定义依赖性

With the second approach, you would then need to execute your build twice: first build would run normally but skipping tests ( mvn clean install ), second build you would activate the profile and starting from the test phase ( mvn test -Pprofile-name ). 使用第二种方法,您将需要执行两次构建:第一个构建可以正常运行,但是会跳过测试( mvn clean install ),第二个构建则可以激活配置文件并从测试阶段开始( mvn test -Pprofile-name ) 。

The first approach (multi-module project) is definitely more recommended. 绝对推荐使用第一种方法(多模块项目)。

No, it's not, because maven will just select the later version. 不,不是,因为maven只会选择更高的版本。 That's because at runtime, java can't use both versions - if you had both in the classpath, the first one loaded would "win" and the other wouldn't be used. 那是因为在运行时,java不能同时使用这两个版本-如果您在类路径中同时使用了这两个版本,则第一个加载的将“获胜”,而另一个则不会被使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM