【问题标题】:Maven build assembly with dependenciesMaven 构建具有依赖项的程序集
【发布时间】:2012-11-16 22:15:41
【问题描述】:

我有独立的 java 应用程序,我想将其打包为:myapp.jar。并将所有依赖的 jar 复制到“备用文件夹”。理想情况下,我希望 maven 更新 META-INF 文件以将所有类路径依赖项 jar 条目添加到其中。

例如,如果我的项目引用 commons.jar,并且当我使用此插件构建程序集时,它将所有 .class 文件和包从 commons.jar 复制到 myappjar-with-dependencies.jar。

maven 程序集插件的问题是将所有依赖项解压缩到 myappjar-with-dependencies.jar 中。

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

【问题讨论】:

  • “maven 程序集插件的问题将所有依赖项解压缩到 myappjar-with-dependencies.jar。”是什么意思?这句话看不懂。
  • 例如,如果我的项目引用了commons.jar,并且当我使用这个插件构建程序集时,它会将commons.jar中的所有.class文件和包复制到myappjar-with-dependencies.jar .

标签: java maven


【解决方案1】:

你可以试试

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.core.App</mainClass>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptors>
                    <descriptor>src/assembly/bin.xml</descriptor>
                </descriptors>
            </configuration>
        </plugin>

src/assembly/bin.xml

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <formats>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <unpack>false</unpack>
            <includes>
                <include>${artifact}</include>
            </includes>
        </dependencySet>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <unpack>false</unpack>
            <excludes>
                <exclude>${artifact}</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

运行它

mvn clean package assembly:single

详情:http://maven.apache.org/plugins/maven-assembly-plugin/index.html

【讨论】:

  • 效果很好!,我如何打包一些东西,例如:文件夹 [xxx-SNAPSHOT-1]/lib、[xxx-SNAPSHOT-1]/xxx-SNAPSHOT-1.jar... .. 表示 [lib] 文件夹作为子文件夹和项目 jar 合并到单个文件夹中。
  • 它仍在将所有 jar 包括主 ${artifact} jar 复制到“lib”文件夹中。出于某种原因,它忽略了包含和排除选项。我正在使用 Maven 3.0.4。此外,META-INF 不包含对所有 lib jar 文件的引用。它缺少很多条目。
  • 我的 maven 3.0.4 也是,而且我的构建还可以。如果您通过电子邮件将您的 pom 发送给我,我会尝试找出...
  • 好消息,我快要沮丧了。自己经常做很多错别字。祝你好运
【解决方案2】:

请按照以下步骤操作:

第 1 步: 您应该将常规 Jar 存在于文件夹 - 目标内的 Maven 包中。如果没有,运行方式 > Maven 安装(此时 Maven 构建运行配置中的目标将是 'clean package')

第 2 步: 在 pom.xml 中添加以下脚本

    <build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>Full path with Main class(App) for example com.first.apicode.App</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
  </build>

第 3 步: 将 Maven 构建运行配置中的目标更改为 'clean package assembly:single' 并单击运行

就是这样!

现在您将能够看到目标文件夹中所有依赖项的可运行 JAR,例如 YourArtifactID-0.0.1-SNAPSHOT-jar-with-dependencies.jar

【讨论】:

    猜你喜欢
    • 2014-04-25
    • 2011-11-29
    • 2018-08-22
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2011-02-11
    • 2010-11-05
    • 2016-03-12
    相关资源
    最近更新 更多