java - 使用 Maven 在单独的 jar 中构建和打包应用程序和依赖项

标签 java maven plugins

我正在寻找一个 pom.xml 配置,它将我的应用程序打包在一个 jar 中,并将所有应用程序依赖项打包在另一个 jar 中。我查看了 maven-assembly-plugin 并使用以下配置构建了一个应用程序 jar,然后构建了一个具有所有依赖项的应用程序 jar,但我需要依赖项 jar 不包含我的应用程序:

<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>

谢谢

最佳答案

我已经看到通过在模块中构建应用程序解决了这个问题。这是一个很好的博客:http://giallone.blogspot.com/2012/12/maven-install-missing-offline.html?_sm_au_=iVVnK0HqJZDtb1Hw

对于您的情况,您可以有一个名为“dependencies”的模块,它使用 maven-dependency-plugin 和 maven-assembly-plugin 来复制所有依赖项并将它们打包在一个 jar 中。然后您的应用程序可以引用该 jar,因为它是单独模块中的依赖项。顶层将构建两者。

顶级 pom

.
.
.
    <packaging>pom</packaging>
.
.
    <modules>
        <module>dependencies</module>
        <module>yourApp</module>
    </modules>
.
.
.

依赖pom

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

.
.
.
    <!-- 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>
.
.
.

关于java - 使用 Maven 在单独的 jar 中构建和打包应用程序和依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26666298/

相关文章:

java - 我们如何向左旋转数组?

java - 为什么java注解语法有()[括号]?

spring - 为公司环境设置内部Maven存储库

macos - Mac的Mvvmcross和Mac的插件支持(例如sqlite)?

java - 赋值的左边不是变量?

JavaFX如何使文本区域中的文本带有下划线?

java - Eclipse 和 maven-war-plugin 爆炸式增长

maven - fb-contrib 错误显示为未知错误模式

linux - Golang 插件热重载

c++ - 行为根据它所链接的类而改变的程序