I am fighting with maven to include a managed dependency with \'provided\' scope into tar file by using the maven-assembly-plugin.
I use super parent pom file as a base
This can be done using the Assembly plugin.
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>
Then just enable it in your 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>
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
You can simply define the scope into your assembly descriptor.
You can override a managed dependency by declaring it inside a <dependencyManagement>
tag in the POM where you want it to be overridden.
In your case, you should add the following to your child pom:
<dependencyManagement>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<scope>provided</scope>
</dependency>
</dependencyManagement>
Note that this overrides everything declared in the parent POM's dependency management, i.e. you cannot leave version
or scope
undeclared and expect it to be inherited.
it is not possible to override the 'provided' scope in maven.
To solve this issue, I declared a variable in the parent pom that will define the artifact scope. In order to override the scope the only thing that has to be done, is to set new value for the variable in the inherited pom.
See example below:
parent 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>
Now in the child pom, just declare the variable again:
<properties>
<log4j.scope>runtime</log4j.scope>
</properties>
I encountered a similar issue, trying to assemble a project with a dependency with scope "provided". I found a workaround for this issue: