问题描述
我正在与maven进行斗争,以通过使用maven-assembly-plugin将具有提供的"作用域的托管依赖项包含到tar文件中.
I am fighting with maven to include a managed dependency with 'provided' scope into tar file by using the maven-assembly-plugin.
我将超级父pom文件用作所有项目的基础.大多数项目将部署在应用程序服务器下,因此在超级父pom下声明了两个常见的依赖项.下面是超级父级的相关管理部分:
I use super parent pom file as a base for all of my projects. most of the projects will be deployed under application server so two common dependencies are declared under the super parent pom. below it is the relevant management section from the super parent:
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
log4j.version=2.0.8
在一个继承的项目(它是一个独立的应用程序)中,我正在将maven-assembly-plugin与dependencySets一起使用,以便将依赖库包含到tar文件中.当然,我也想包括log4j库.
in one of the inherited project (which is a standalone application), i am using maven-assembly-plugin with dependencySets in order to include the dependent libraries into a tar file. and of course I want also to include the log4j library.
下面是从超级父级继承的pom:
below is the pom inherited from super parent:
<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文件:
the distribution-app.xml file:
<?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,试图更改范围以进行编译也无法正常工作.我无法更改超级父pom中的声明.
Why maven-assembly-plugin refuse to include the log4j into the tar file?PS, trying to change scope to compile didn't work as well. I can'r change the declaration in the super parent pom.
推荐答案
这可以使用Assembly插件完成.
This can be done using the Assembly plugin.
首先使用以下命令创建一个assembly.xml
:
First create an assembly.xml
with the following:
<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,其中将包含所有编译和提供的资源,以便可以在类路径中对其进行引用.
This will create a yourproject-bin.jar that will include all the compile and provided resources exploded so they can be referenced in a classpath.
java -cp yourproject-bin.jar com.yourcompany.Main
这篇关于如何在Maven-assembly-plugin中将依赖关系包含在“提供的"范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!