如何使用程序集文件打包时复制所有maven依赖项?

时间:2015-02-25 10:45:32

标签: maven dependencies maven-assembly-plugin

我有一个项目A,它的POM中有一些依赖项。 A有一个父项目B和一个祖父项目C.

使用汇编文件打包A时,我想将A的所有必要依赖项放在打包项目的目录中。

目前我只能打包A的POM中的依赖项。 我没有找到一种方法来打包B和C所需的依赖关系。 你知道我怎么做吗?

这是程序集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">  
    <id>bundle</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <!--Include runtime dependencies-->
            <outputDirectory>${ser.depdir}</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <fileSet>
            <!--Get the generated application jar-->
            <directory>${project.build.directory}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <!--Get application resources-->
            <directory>src/main/resources</directory>
            <outputDirectory>conf</outputDirectory>
            <includes>
                <include>*.properties</include>
                <include>*.sh</include>
                <include>log4j2.xml</include>
            </includes>
        </fileSet>
        <fileSet>
            <!--Get misc user files-->
            <directory>${project.basedir}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
            </includes>
        </fileSet>  
    </fileSets>
</assembly>  

和A的POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>group</groupId>
        <artifactId>ser</artifactId>
        <version>1.0.6-SNAPSHOT</version>
    </parent>
    <artifactId>ser-art</artifactId>
    <name>SER ART</name>
    <description>desc</description>
    <properties>
        <ser.depdir>${project.artifactId}-dependencies</ser.depdir>
    </properties>
    <build>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
            <testResource>
                <directory>../config/test</directory>
            </testResource>
        </testResources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>mainClass</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>${ser.depdir}/</classpathPrefix>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>./conf/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <excludes>
                        <exclude>*.properties</exclude>
                        <exclude>*.sh</exclude>
                        <exclude>log4j2.xml</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>${basedir}/src/main/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/${ser.depdir}</outputDirectory>
                            <includeScope>runtime</includeScope>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>group</groupId>
            <artifactId>ser-commons</artifactId>
            <version>1.0.6-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>group</groupId>
            <artifactId>utilities-lib</artifactId>
            <version>1.0.6-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

1 个答案:

答案 0 :(得分:0)

  1. 从头顶解决方法可能是你使用“maven依赖插件”复制阶段将所有内容复制到某个目录,然后使用程序集插件的“文件集”描述符。但这会让你的pom看起来很脏。

  2. 我没试过,但程序集插件支持传递依赖的打包。这是您案例中的正确解决方案。