英文:
How to change behavior according to maven/gradle scope?
问题
如何使maven/gradle更改其按范围导出的内容?这可能吗?
就像这样使用:
<dependency>
<groupId>org.blahblah</groupId>
<artifactId>anything</artifactId>
<version>5.8</version>
<scope>test</scope>
</dependency>
通过使用另一种方式获得不同的二进制文件
[...]
<scope>compile</scope>
[...]
英文:
How to make maven/gradle package change what it’s exported by scope? Is it possible?
Like, use like this
<dependency>
<groupId>org.blahblah</groupId>
<artifactId>anything</artifactId>
<version>5.8</version>
<scope>test<scope/>
</dependency>
To get different binaries by use this another way
[...]
<scope>compile<scope/>
[...]
答案1
得分: 0
是的和不是 - 有点。
一般的准则是每个 pom.xml 创建一个构件(artifact) - 大多数工具与这个概念相当契合。一旦你超越了这个范围,有时会发生一些奇怪的事情。仅通过 scope 更改 jar 是不可能的,据我所知。这也会让人们感到困惑。可能会使故障排除变得非常困难。
但是有一个变通方法。正如你提到的测试:jar 插件允许你将 src/test/java 中的类导出为 test-jar,并将其用作依赖项,指定一个类型。
参见 如何创建一个 test-jar。
我认为相同的机制可以用于其他事物。
还有一个分类器的概念(通常用于源代码、javadoc 等等)。参见 这个问题。
尽管这些事情在命令行上往往可以与 Maven 一起工作,但如果你过于深入地使用类型和分类器,IDEs 有时会开始表现得有点奇怪。
英文:
yes and no - sort of.
The general guideline is to create one artifact per pom.xml - most tools work quite nice with this concept. As soon as you go beyond sometimes funky stuff happens. Changing the jar only by scope isn't possible afaik. It would also confuse people a lot. And probably will make troubleshooting very difficult.
But there is a workaround. As you mentioned tests: the jar plugin allows you to export the classes in src/test/java as test-jar and use that as dependency specifying a type.
I assume the same mechanism with the type can be used for other things as well.
There is also the concept of classifiers (this is usually used for sources, javadoc and things like that). See this question.
While these things tend to work with maven on the command line, IDEs sometimes start to behave a bit weird if you push type and classifier usage too far.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论