根据Maven/Gradle的范围如何更改行为?

huangapple go评论99阅读模式
英文:

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

&lt;dependency&gt;
  &lt;groupId&gt;org.blahblah&lt;/groupId&gt;
  &lt;artifactId&gt;anything&lt;/artifactId&gt;
  &lt;version&gt;5.8&lt;/version&gt;
  &lt;scope&gt;test&lt;scope/&gt;
&lt;/dependency&gt;

To get different binaries by use this another way

[...]
  &lt;scope&gt;compile&lt;scope/&gt;
[...]

答案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.

See How to create a test-jar.

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.

huangapple
  • 本文由 发表于 2020年4月8日 14:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/61094200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定