maven不重新打包依赖

时间:2012-01-04 09:23:58

标签: java maven maven-assembly-plugin

我使用maven程序集插件将所有依赖项收集到一个jar文件中。如何告诉maven不要重新打包依赖项并将它们作为jar文件包含在生成的jar中?

目前我使用以下插件配置。

<plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>package.Program</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>

想要jar文件的结构:

my-jar-with-dependencies.jar
|-dependency1.jar
|-|-class1.class
|-dependency2.jar
|-|-class2.class
|-...........

而不是

my-jar-with-dependencies.jar
|-class1.class
|-class2.class
|-.............

1 个答案:

答案 0 :(得分:0)

如果我正确理解了这个问题,你想指定何时应该运行程序集插件。我应该考虑创建build profile并将程序集插件配置添加到新配置文件中。

在pom.xml中添加:

<project>
...
 <profiles>
    <profile>
        <id>myprofile</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>package.Program</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
 </profiles>
...
</project>

当你想要maven运行程序集插件时,你可以使用-P开关到'mvn'脚本,如下所示:

mvn -Pmyprofile clean package