首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Maven在单独的jars中构建和打包应用程序和依赖项

使用Maven在单独的jars中构建和打包应用程序和依赖项
EN

Stack Overflow用户
提问于 2014-10-31 01:42:28
回答 1查看 2.6K关注 0票数 7

我正在寻找一个pom.xml配置,它可以将我的应用程序打包到一个jar中,而所有的应用程序依赖项都打包在另一个jar中。我查看了maven程序集插件,通过下面的配置,我能够构建一个应用程序jar,然后构建一个包含所有依赖项的应用程序jar,但是我需要依赖jar才能不包含我的应用程序:

代码语言:javascript
复制
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <appendAssemblyId>false</appendAssemblyId>
    </configuration>
    <executions>
        <execution>
            <id>jar-with-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>attached</goal>
            </goals>
        </execution>
    </executions>
</plugin>

谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-10-31 03:47:39

我已经看到通过在模块中构建应用程序来解决这个问题。这里有一个很好的博客:=iVVnK0HqJZDtb1Hw

对于您的情况,您可以有一个名为“依赖项”的模块,该模块使用maven依赖插件和maven程序集插件复制所有依赖项,并将它们打包到一个jar中。然后,应用程序可以引用jar,因为它是独立模块中的依赖项。高层将两者兼而有之。

顶级pom

代码语言:javascript
复制
.
.
.
    <packaging>pom</packaging>
.
.
    <modules>
        <module>dependencies</module>
        <module>yourApp</module>
    </modules>
.
.
.

依赖项pom

代码语言:javascript
复制
.
.
.
    <dependencies>
        <dependency>
            <groupId>some.group.id</groupId>
            <artifactId>yourFirstDependency</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>some.group.id2</groupId>
            <artifactId>yourSecondDependency</artifactId>
            <version>1.1.4</version>
        </dependency>
    <dependencies>

    <build>
        <defaultGoal>install</defaultGoal>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
.
.
.

主要应用程序pom

代码语言:javascript
复制
.
.
.
    <!-- Reference your dependency module here as the first in the list -->
    <dependencies>
        <dependency>
            <groupId>your.group.id</groupId>
            <artifactId>yourDependencyJarName</artifactId>
            <version>1.0</version>
        </dependency>
        .
        .
    </dependencies>
.
.
.
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26666298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档