我正在与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>
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>
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>
为什么maven-assembly-plugin拒绝将log4j包含在tar文件中? PS,尝试将范围更改为编译也不起作用。我无法改变超级父母的声明。
答案 0 :(得分: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>
然后只需在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>
这将创建一个yourproject-bin.jar,它将包含所有已编译和提供的资源,以便在类路径中引用它们。
java -cp yourproject-bin.jar com.yourcompany.Main
答案 1 :(得分:10)
无法覆盖提供的&#39;范围在maven。
为了解决这个问题,我在父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>
现在在子pom中,再次声明变量:
<properties>
<log4j.scope>runtime</log4j.scope>
</properties>
答案 2 :(得分:4)
您可以通过在POM中要覆盖它的<dependencyManagement>
标记内声明来覆盖托管依赖关系。
在您的情况下,您应该将以下内容添加到您的孩子pom:
<dependencyManagement>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<scope>provided</scope>
</dependency>
</dependencyManagement>
请注意,这会覆盖父POM依赖关系管理中声明的所有内容,即您不能保留version
或scope
未声明并期望它被继承。
答案 3 :(得分:0)
您只需将范围定义到assembly descriptor。
即可答案 4 :(得分:0)
我遇到了类似的问题,试图组建一个带有“提供”范围的依赖项目。 我找到了解决此问题的方法: