如何在maven-assembly-plugin中包含与"提供"范围的依赖关系

guy*_*ymi 22 maven

我正在与maven战斗,通过使用maven-assembly-plugin将一个带有'提供'范围的托管依赖包含到tar文件中.

我使用超级父pom文件作为我所有项目的基础.大多数项目将部署在应用程序服务器下,因此在超级父pom下声明了两个常见的依赖项.下面是超级母公司的相关管理部门:

http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xxx.integration</groupId>
    <artifactId>super-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.1.3</version>
    <name>super parent</name>
    <url>http://maven.apache.org.check</url>
.
.
.
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
Run Code Online (Sandbox Code Playgroud)

log4j.version = 2.0.8

在一个继承的项目(这是一个独立的应用程序)中,我使用带有dependencySets的maven-assembly-plugin,以便将依赖库包含在tar文件中.当然我也希望包含log4j库.

下面是从超级父母继承的pom:

<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/maven-v4_0_0.xsd">
    <parent>
        <groupId>com.xxx.integration</groupId>
        <artifactId>super-parent</artifactId>
        <version>1.1.3</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>plugin-cc-checker</artifactId>
    <name>plugin-cc-checker</name>
    <version>2.1</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>com.orca.integration</groupId>
                        <artifactId>integration-assembly-descriptor</artifactId>
                        <version>1.1.1</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>make-assembly-according-to-distribution-xml</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>${artifactId}</finalName>
                            <!-- This is where we use our shared assembly descriptor -->
                            <descriptors>
                                <descriptor>distribution-app.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xerces</artifactId>
            <version>${xerces.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging-api</artifactId>
            <version>${commons-logging-api.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>excalibur</groupId>
            <artifactId>excalibur-i18n</artifactId>
            <version>${excalibur-i18n.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.snmp4j</groupId>
            <artifactId>snmp4j</artifactId>
            <version>${snmp4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

distribution-app.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
    <!-- Add module dependencies and the jar that is created in the packaging 
        phase. Product name will be <project name>-app-<version no>.tar -->
    <id>app-${version}</id>
    <formats>
        <format>tar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>resources/app</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <excludes>
                <!-- Since there is a bug in xalan 2.7.1 all applications required to 
                    use xalan-orca jar file -->
                <exclude>xalan:xalan</exclude>
            </excludes>
            <!-- includes> <include>*</include> </includes-->
        </dependencySet>
    </dependencySets>
    <moduleSets>
        <moduleSet>
            <binaries>
                <outputDirectory>/guy</outputDirectory>
                <includes>
                    <include>log4j:log4j</include>
                </includes>
            </binaries>
        </moduleSet>
    </moduleSets>
</assembly>
Run Code Online (Sandbox Code Playgroud)

为什么maven-assembly-plugin拒绝将log4j包含在tar文件中?PS,尝试将范围更改为编译也不起作用.我无法更改超级父母pom中的声明.

Jus*_*KSU 12

这可以使用Assembly插件完成.

首先创建一个assembly.xml具有以下内容:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>bin</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <unpack>true</unpack>
            <scope>runtime</scope>
        </dependencySet>
        <dependencySet>
            <unpack>true</unpack>
            <scope>provided</scope>
        </dependencySet>
    </dependencySets>
</assembly>
Run Code Online (Sandbox Code Playgroud)

然后在你的启用它 pom.xml

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptor>src/main/assembly/assembly.xml</descriptor>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

这将创建一个yourproject-bin.jar,其中将包括所有的编译和提供的资源展开,使他们能够在classpath中被引用.

java -cp yourproject-bin.jar com.yourcompany.Main
Run Code Online (Sandbox Code Playgroud)


guy*_*ymi 10

无法覆盖maven中的"提供"范围.

为了解决这个问题,我在父pom中声明了一个定义工件范围的变量.为了覆盖范围,唯一需要做的就是为继承的pom中的变量设置新值.

见下面的例子:

父母pom:

    <properties>
            <log4j.version>1.2.8</log4j.version>
            <log4j.scope>provided</log4j.scope>
    </properties>
.
.
.
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
                <scope>${log4j.scope}</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
Run Code Online (Sandbox Code Playgroud)

现在在子pom中,只需再次声明变量:

<properties>
        <log4j.scope>runtime</log4j.scope>
</properties>
Run Code Online (Sandbox Code Playgroud)

  • 可以通过在子POM中管理它来覆盖托管依赖项.请参阅下面的答案. (2认同)