简体   繁体   English

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

[英]How to copy all maven dependencies when packaging using assembly file?

I have a project A that has some dependencies in its POM. 我有一个项目A,它的POM中有一些依赖项。 A has a parent project B and a grand-parent project C. A有一个父项目B和一个祖父项目C.

When packaging A, using an assembly file, I'd like to put all necessary dependencies for A inside a directory in the packaged project. 打包A时,使用程序集文件,我想将A的所有必要依赖项放在打包项目的目录中。

Currently the I can only package the dependencies that are in A's POM. 目前,我只能打包A的POM中的依赖项。 I didn't find a way to package B's and C's dependencies that are necessary to A. Do you know how I could do that? 我没有找到一种方法来打包B和C所需的依赖关系A.你知道我怎么做吗?

Here's the assembly XML: 这是程序集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>  

And A's POM: 和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. A workaround from top of my head could be that you use "maven dependency plugin" copy phase to copy everything to a certain dir and then using "fileset" descriptor of assembly pluging. 从头到尾的解决方法可能是你使用“maven依赖插件”复制阶段将所有内容复制到某个目录,然后使用程序集插件的“文件集”描述符。 But this will make your pom look dirty. 但这会让你的pom看起来很脏。

  2. I have not tried but assembly plugin supports the packaging of transitive dependency. 我没有尝试,但程序集插件支持传递依赖的包装。 That is the correct solution in your case. 这是您案例中的正确解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM