Maven构建具有依赖关系的程序集

时间:2012-11-28 22:34:25

标签: java maven

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

例如,如果我的项目引用了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>

2 个答案:

答案 0 :(得分:19)

您可以尝试

        <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 /组件/ 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

答案 1 :(得分:0)

请按照以下步骤操作:

步骤1: 您应该将常规Jar放在文件夹-target中的Maven软件包中。如果不是,请以> Maven Install的身份运行(此时,Maven Build Run配置中的目标将是“干净的程序包”)

步骤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 Build Run配置中的目标更改为“ clean package assembly:single”,然后单击Run

仅此而已!

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