[英]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 多模块项目,其结构如下:
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: 这也意味着:
<skip>${skip.tests}</skip>
) which would have as default value true
作为默认构建的一部分,您将需要跳过测试,将Maven Surefire插件配置为通过默认值为true
的属性(即<skip>${skip.tests}</skip>
) 跳过测试 。 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.