【问题标题】:Build and package application and dependencies in separate jars with Maven使用 Maven 在单独的 jar 中构建和打包应用程序和依赖项
【发布时间】:2014-10-31 01:42:28
【问题描述】:

我正在寻找一种 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>

谢谢

【问题讨论】:

    标签: java maven plugins


    【解决方案1】:

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

    对于您的情况,您可以有一个名为“依赖项”的模块,它使用 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>
    .
    .
    .
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 2010-12-16
      相关资源
      最近更新 更多