Maven - 添加jar-with-dependencies作为依赖 [英] Maven - Add jar-with-dependencies as a dependecy

查看:477
本文介绍了Maven - 添加jar-with-dependencies作为依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题..

我想在依赖关系打包的Maven jar中添加依赖项。



详细信息..

我有一个多模块Maven项目,其中一个模块依赖于本机库等。作为其构建的一部分,它将其依赖关系包含在 jar-with-dependencies 中,如下所示:

 <插件> 
< artifactId> maven-assembly-plugin< / artifactId>
< version> 2.4< / version>
< configuration>
< descriptorRefs>
< descriptorRef> jar-with-dependencies< / descriptorRef>
< / descriptorRefs>
< archive>
< manifest>
< mainClass> $ {mainClass}< / mainClass>
< / manifest>
< / archive>
< / configuration>
<执行>
< execution>
< id> make-assembly< / id>
< phase> package< / phase>
< goals>
< goal> single< / goal>
< / goals>
< / execution>
< / executions>
< / plugin>

所有的好,我建立两个罐子:

  seaniscool-0.0.1-SNAPSHOT.jar 
seaniscool-0.0.1-SNAPSHOT-jar-with-dependencies.jar

但是,我想在同一个项目的另一个模块中使用这个工件。如果我只是添加模块作为依赖,我得到的jar没有包含本机库。



我可以复制构建配置,以将本机库包含在第二个模块还有,它不是非常广泛,但不愿意。



任何想法如何添加 jar-with-dependencies 作为依赖,因此依赖于包含的库?



有些想法..

我知道我可以使用Maven可以参考的测试类来构建一个单独的jar:



在第一个模块中:

 <插件> 
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-jar-plugin< / artifactId>
< version> 2.2< / version>
<执行>
< execution>
< goals>
< goal> test-jar< / goal>
< / goals>
< / execution>
< / executions>
< / plugin>

在第二个模块中:

 <依赖性> 
< groupId> my.group.id< / groupId>
< artifactId> my.artifact.id< / artifactId>
< version> my.version< / version>
< type> test-jar< / type>
< scope> test< / scope>
< / dependency>

这个概念可以在这里传输吗?

解决方案

您可以使用 maven分类器进行此操作。 Classfiers被使用,所以maven模块可以从同一个源构建多个人工制品。示例是jdk1.6或1.7版本,甚至源代码和javadoc jars maven可以构建。



所以尝试这样:

 <依赖性> 
< groupId> yourID< / groupId>
< artifactId> seaniscool< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< classifier> jar-with-dependencies< / classifier>
< / dependency>

如果您想将您的classfier重命名为更好的名称,如Native或完整或任何其他内容在 maven shade plugin 中,还可以使用依赖关系构建jar,但可以进行更多的控制。 >

Question..
I'd like to add a dependency on a Maven jar packaged with it's dependencies.

Details..
I have a multi-module Maven project in which one of the module depends on native libraries and the like. As part of it's build, it packages up it's dependencies into a jar-with-dependencies as shown here:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>${mainClass}</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

All good, I get two jars when I build:

seaniscool-0.0.1-SNAPSHOT.jar
seaniscool-0.0.1-SNAPSHOT-jar-with-dependencies.jar

However, I'd like to use this artifact in another module of the same project. If I simply add the module as a dependency, I get the jar without the native libraries included.

I could duplicate the build configuration to include the native libraries in the 2nd module also, it's not very extensive, but would prefer not to.

Any ideas how I can add the jar-with-dependencies as a dependency and thus depend on the libraries included?

Some thoughts..
I know I can build a separate jar with test classes that Maven can reference:

In 1st module:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <goals>
                <goal>test-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

In 2nd module:

<dependency>
    <groupId>my.group.id</groupId>
    <artifactId>my.artifact.id</artifactId>
    <version>my.version</version>
    <type>test-jar</type>
    <scope>test</scope>
</dependency>

Is this concept transferrable here perhaps?

解决方案

You can do this with a maven classifier. Classfiers are used so a maven module can build multiple artefacts from the same source. Examples are jdk1.6 or 1.7 version or even the source and javadoc jars maven can build.

So try this:

<dependency>
  <groupId>yourID</groupId>
  <artifactId>seaniscool</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <classifier>jar-with-dependencies</classifier>
</dependency>

If you want to rename your classfier to a better name like withNative or complete or anything else have a look at the maven shade plugin which can also build jars with dependencies but allows some more control.

这篇关于Maven - 添加jar-with-dependencies作为依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆