Java Mavens 依赖声明分类器属性的目的是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20909634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the purpose of Mavens dependency declarations classifier property?
提问by pushya
I have a pom.xml file and in that i see that their are 3 dependencies referenced for same <artifactId>
the difference are in tags
我有一个 pom.xml 文件,我看到它们是 3 个引用的依赖项<artifactId>
,不同之处在于标签
<classifier>sources</classifier>
<classifier>javadoc</classifier>
I have deleted the dependencies that had the SOURCES/JAVADOC
and only kept one dependency. I tested my application and every thing work fine.
我已经删除了具有SOURCES/JAVADOC
并且只保留一个依赖项的依赖项。我测试了我的应用程序,一切正常。
What is the purpose of using this classifier tag? and why i need to duplicate dependencies twice for adding <classifier>
tag with SOURCES/JAVADOC
.
使用这个分类器标签的目的是什么?以及为什么我需要复制依赖项两次才能添加<classifier>
带有SOURCES/JAVADOC
.
<dependency>
<groupId>oauth.signpost</groupId>
<artifactId>signpost-commonshttp4</artifactId>
<version>1.2.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>oauth.signpost</groupId>
<artifactId>signpost-commonshttp4</artifactId>
<version>1.2.1.2</version>
<type>jar</type>
***<classifier>javadoc</classifier>***
<scope>compile</scope>
</dependency>
<dependency>
<groupId>oauth.signpost</groupId>
<artifactId>signpost-commonshttp4</artifactId>
<version>1.2.1.2</version>
<type>jar</type>
***<classifier>sources</classifier>***
<scope>compile</scope>
</dependency>
采纳答案by Biswajit
回答by vishal sahasrabuddhe
Example for Classifier
As a motivation for this element, consider for example a project that offers an artifact targeting JRE 1.8 but at the same time also an artifact that still supports JRE 1.7. The first artifact could be equipped with the classifier jdk18 and the second one with jdk14 such that clients can choose which one to use.
分类器示例
作为此元素的动机,请考虑例如一个项目,该项目提供了一个面向 JRE 1.8 的工件,但同时也是一个仍支持 JRE 1.7 的工件。第一个工件可以配备分类器 jdk18,第二个工件配备 jdk14,以便客户端可以选择使用哪个。
Another common use case for classifiers is the need to attach secondary artifacts to the project's main artifact. If you browse the Maven central repository, you will notice that the classifiers sources and javadoc are used to deploy the project source code and API docs along with the packaged class files.
分类器的另一个常见用例是需要将次要工件附加到项目的主要工件。如果您浏览 Maven 中央存储库,您会注意到分类器源和 javadoc 用于部署项目源代码和 API 文档以及打包的类文件。
回答by Mr.Q
It allows distinguishing two artifacts that belong to the same POM but were built differently, and is appended to the filename after the version.
它允许区分属于同一 POM 但构建方式不同的两个工件,并附加到版本后的文件名。
For example if you have other artifacts in your repository (docs, sources ...) you can reference them and add them to your project as dependency.
in this code by adding the <classifier>sources</classifier>
we are getting the sources.jar from repository.
例如,如果您的存储库中有其他工件(文档、源...),您可以引用它们并将它们作为依赖项添加到您的项目中。在此代码中添加<classifier>sources</classifier>
我们正在从存储库获取sources.jar。
<dependency>
<groupId>oauth.signpost</groupId>
<artifactId>signpost-commonshttp4</artifactId>
<version>1.2.1.2</version>
<type>jar</type>
***<classifier>sources</classifier>***
<scope>compile</scope>
</dependency>
actually It lets you locate your dependencies with the further level of granularity.
实际上,它可以让您以更高的粒度级别定位您的依赖项。
回答by Zhu Fei
According to the following: https://blog.packagecloud.io/eng/2017/03/09/how-does-a-maven-repository-work/classifier tag has implies "Secondary Artifact", which its "transitive dependency" will be cut off! Thus classifier tag not only change "Maven Coordinate" by $artifactId-$version-$classifier.jar!
根据以下内容:https: //blog.packagecloud.io/eng/2017/03/09/how-does-a-maven-repository-work/分类器标签暗示了“辅助工件”,它的“传递依赖”将被切断!因此,分类器标签不仅会通过 $artifactId-$version-$classifier.jar 更改“Maven 坐标”!
回答by pirho
Yet another more pragmatic answer by an example to help to understand the usefulness of classifier
better.
另一个更务实的答案是通过一个例子来帮助理解classifier
更好的用处。
Suppose you have a need for two versions of an?artifact: for openjpa
and for eclipselink
- say because jar contains entities that are needed to be enhanced JPA implementation specifically.
假设您需要两个版本的工件:foropenjpa
和 for eclipselink
- 比如说因为 jar 包含需要专门增强 JPA 实现的实体。
You might have some different handling for these builds defined in Maven profiles and the profiles used then have also property <classifier />
.
对于 Maven 配置文件中定义的这些构建,您可能有一些不同的处理方式,然后使用的配置文件也具有 property <classifier />
。
To build the differently classified versions, in pom
the maven-jar-plugin
would then be configured followingly
要建立不同的分类版本中,pom
在maven-jar-plugin
将被从动地配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<classifier>${classifier}</classifier>
</configuration>
</plugin>
Installing both would result to files in repo something like this:
安装两者会导致 repo 中的文件如下所示:
org/example/data/1.0.0/data-1.0.0.pom
org/example/data/1.0.0/data-1.0.0-openjpa.jar
org/example/data/1.0.0/data-1.0.0-eclipselink.jar
org/example/data/1.0.0/data-1.0.0.pom
org/example/data/1.0.0/data-1.0.0-openjpa.jar
org/example/data/1.0.0/data-1.0。 0-eclipselink.jar
Now it would be only matter of classifier
to which one use, so
现在只是classifier
使用哪一种的问题,所以
<dependency>
<groupId>org.example</groupId>
<artifactId>data</artifactId>
<version>1.0.0</version>
<classifier>[openjpa|eclipselink]</classifier>
</dependency>