I need to copy just one dependency and all its transitive dependencies to a specified folder.
I know i can exclude artifacts with "excludeArtifactIds", but I also need to exclude the transitive dependencies of those artifacts, which, apparently "excludeArtifactIds" does not do.
Is there a way of doing this?
It appears that the Maven dependency plugin is not designed for this as they closed one request for this functionality as "WONTFIX" and another request has been OPEN since 2007.
However, you can use the maven-assembly-plugin to accomplish a similar task.
Below I've attached two sample POM's. The first is the dependent project (the one you wanted to copy) which itself has one dependency (for example). The second is the aggregate project where you are copying the other project and it's dependency to. I've also attached the assembly desriptor file that you'll use to copy the dependency.
Essentially, this will copy the first project and it's one dependency to the target/dest (configurable) directory of the second project.
First POM (dependent project): /sample-dependency/pom.xml
<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>
<groupId>sample-dependency</groupId>
<artifactId>sample-dependency</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>
Second POM (aggregating project): /sample-dependency-aggregator/pom.xml
<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>
<groupId>sample-dependency-aggregator</groupId>
<artifactId>sample-dependency-aggregator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sample-dependency-aggregator</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>aggregate</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptor>src/main/assembly/default.xml</descriptor>
</configuration>
</execution>
</executions>
<configuration>
<attach>false</attach>
<finalName>dest</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>sample-dependency</groupId>
<artifactId>sample-dependency</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Assembly descriptor : /sample-dependency-aggregator/src/main/assembly/default.xml
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.1 http://maven.apache.org/xsd/assembly-1.1.1.xsd ">
<id>default</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<includes>
<include>sample-dependency:sample-dependency</include>
</includes>
<useTransitiveDependencies>true</useTransitiveDependencies>
<useTransitiveFiltering>true</useTransitiveFiltering>
</dependencySet>
</dependencySets>
</assembly>
How about setting excludeTransitive to true?
Related
I am new to Maven and starting moving my project to maven. I have created the following POM.
when i issue install command, it gives me FATAL ERROR.
I am using maven 2.2.1 version and JDK 1.5.
My POM is as follows:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<prerequisites>
<maven>2.2.1</maven>
</prerequisites>
<groupId>my.project.group</groupId>
<artifactId>my.artifact</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6.SEC01</version>
</dependency>
</dependencies>
</project>
Thanks for any help.
You are missing a mandatory tag from your project address structure groupId:artifactId:version
Put the following tag after ... with some version info (i.e. 1.x.x) and try.
<version>1.0.0</version>
Reference : http://maven.apache.org/pom.html
I am trying to rename my artifacts in the repository folder of my eclipse-repository module. At the moment they are auto generated like ...1.0.0.v20130315-1927.jar.
I haven't found any configuration parameter that works. I have tried to use the qualifier setting in the configuration (see tycho-p2-repository-plugin), but it doesn't work.
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>development.statTool</groupId>
<artifactId>Application</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>development.statTool</groupId>
<artifactId>development.statTool.p2</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-repository</packaging>
<properties>
<tycho-version>0.16.0</tycho-version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-p2-repository-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<qualifier>abcd</qualifier>
</configuration>
</plugin>
</plugins>
</build>
</project>
My solution is to use the tycho-packaging-plugin. Only "disadvantage" changing the build qualifier needs to rebuild all modules contained by the repository.
Here the part out of my parent pom.xml:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-packaging-plugin</artifactId>
<version>${tycho.version}</version>
<dependencies>
<dependency>
<groupId>org.eclipse.tycho.extras</groupId>
<artifactId>tycho-buildtimestamp-jgit</artifactId>
<version>${tycho-extras.version}</version>
</dependency>
</dependencies>
<configuration>
<strictBinIncludes>false</strictBinIncludes>
<format>'rev${rev}-'yyyyMMdd-HHmm</format>
</configuration>
</plugin>
try this:
mvn -Dtycho.mode=maven org.eclipse.tycho:tycho-versions-plugin:set-version -DnewVersion=13.3.0.1-SNAPSHOT
or simply search text "1.0.0-SNAPSHOT" and replace it with "13.3.0.1" using Actual Search and Replace tool.
I have finally succeeded in getting Maven to zip together a bunch of jars using an assembly file and install it to my local repository. That was difficult enough...
Now my goal is to configure another maven project so that when I do "mvn test", it will pull in that zip, unpack it, and run tests from the jars within that zip file. Does anyone know how to do this?
Here is the POM for the assembly project:
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pason</groupId>
<artifactId>RigFocusOnDataHub</artifactId>
<name>RigFocusOnDataHub</name>
<version>12.2.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptors>
<descriptor>RigFocusOnDH.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>zip</id>
<!-- this is used for inheritance merges -->
<phase>package</phase>
<!-- append to the packaging phase. -->
<goals>
<goal>single</goal>
<!-- goals == mojos -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Here is the POM for the second project. Unfortunately, instead of downloading the zip file for RigFocusOnDataHub, it just fetches the jars for all of RigFocusOnDataHub's dependencies from the local repo.
<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>
<groupId>com.pason</groupId>
<artifactId>RigFocusDHSystemTest</artifactId>
<version>12.2.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.pason</groupId>
<artifactId>MurphyRigFocus</artifactId>
<version>2.0.0-SNAPSHOT</version>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>com.pason</groupId>
<artifactId>RigFocusOnDataHub</artifactId>
<version>12.2.0-SNAPSHOT</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>process-test-classes</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.pason</groupId>
<artifactId>MurphyRigFocus</artifactId>
<version>2.0.0-SNAPSHOT</version>
<type>test-jar</type>
<outputDirectory>${project.build.directory}/tests/MurphyRigFocus</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>com.pason</groupId>
<artifactId>RigFocusOnDataHub</artifactId>
<version>12.2.0-SNAPSHOT</version>
<type>zip</type>
<outputDirectory>${project.build.directory}/tests/MurphyRigFocus</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<testClassesDirectory>${project.build.directory}/tests/MurphyRigFocus</testClassesDirectory>
<reportsDirectory>${project.build.directory}/surefire-reports/MurphyRigFocus</reportsDirectory>
<includes>
<include>**/*IT.*</include>
</includes>
<argLine>-Djava.library.path=${basedir}/target/classes/</argLine>
<forkMode>pertest</forkMode>
</configuration>
</plugin>
</plugins>
</build>
You would need to:
extract the jars from the zip - this is easy enough with maven-dependency-plugin
cut transitive dependencies so your jars don't end up in the path twice - you can do that at the source with maven-shade-plugin or in the test project itself with dependencies exclusions
add the jars to your test classpath, there are many ways to do that, I would try to use additional parameters in surefire configuration first
I'm using almost same POM for both my projects, they are on the same workspace but they are not related at all, they are related however because in both I use spring and jboss. Here is the 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springinaction.hello</groupId>
<artifactId>spring-in-action</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-in-action</name>
<url>http://maven.apache.org</url>
<properties>
<jboss.ome>C:\jboss-5.1.0.GA\server\default\deploy</jboss.ome>
<springversion>2.5.3</springversion>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>${springversion}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
<configuration>
<warName>spring-book</warName>
<outputDirectory>${jboss.ome}</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
What I want to achieve with this POM is that I want to name my war when built spring-book.war and copy it to Jboss location I specified. Now in my first project this works it does exactly what I requested but in other it does not. I changed springversion and jboss home properties variable but everything remains the same, what can I do ? The project builds and all, everything is working perfectly just I don't want to copy everytime in my jboss dir and previously remove the old war, it takes about 20sec on each source code change its a lot
Problem spotted at this line:
<packaging>jar</packaging>
You're not using the right packaging, it should be:
<packaging>war</packaging>
After this change the war plugin should get called and things should work like in the other project :)
You could leave the output directory at its default, and use a profile instead with the maven jboss plugin. It has a hard-deploy target which copies your artifact to the deploy directory. If it's in a profile, you can activate it when (and only when) you want.
Moreover, with the antrun plugin, you can also delete the old war file before copying over the new one (this is useful when the war filename includes the version, but in your case may not be needed).
<profiles>
<profile>
<id>deploy</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>remove-old-war</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete>
<fileset dir="${jboss.ome}"
includes="*.war"/>
</delete>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<executions>
<execution>
<id>redeploy-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>hard-deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
You can then activate the profile with
mvn -Pdeploy install
My assembly descriptor for module (APP1) is:
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>report</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<includes>
<include>*-APP2</include>[trying to refer to another module ie module-APP2]
</includes>
<sources>
<fileSets>
<fileSet>
<directory>/</directory>
<includes>
<include>**/target</include>
</includes>
</fileSet>
</fileSets>
<excludeSubModuleDirectories>false</excludeSubModuleDirectories>
<outputDirectoryMapping>/</outputDirectoryMapping>
</sources>
</moduleSet>
</moduleSets>
</assembly>
When I am running the mvn install cmd , I'm getting
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o '*-APP2'
where have I gone wrong?
I modified as :
<?xml version="1.0" encoding="UTF-8"?><assembly>
<id>report</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<includes>
<include>sampleMaven:module-APP2</include>
</includes>
<sources>
<fileSets>
<fileSet>
<directory>/</directory>
<includes>
<include>target/*</include>
</includes>
</fileSet>
</fileSets>
<excludeSubModuleDirectories>false</excludeSubModuleDirectories>
<outputDirectoryMapping>/</outputDirectoryMapping>
</sources>
</moduleSet>
</moduleSets>
</assembly>
still getting :
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o 'sampleMaven:module-APP2'
Updated on 18/sep:
Main proj pom.xml-->
http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
sampleMaven
anu
0.0.1-SNAPSHOT
pom
APP1
<module>APP2</module>
2)For APP1, the pom.xml is-->
<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">
anu
sampleMaven
0.0.1-SNAPSHOT
4.0.0
sampleMaven
APP1
APP1
0.0.1-SNAPSHOT
pom
../APP2
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-3</version>
<executions>
<execution>
<id>assemblyone</id>
<phase>compile</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>App1</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>${basedir}/src/main/resources/assemblies/report.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> ...
3)Assembly descriptor is -->
report
jar
false
<sources>
<fileSets>
<fileSet>
<directory>/</directory>
<includes>
<include>target/*</include>
</includes>
</fileSet>
</fileSets>
<excludeSubModuleDirectories>false</excludeSubModuleDirectories>
<outputDirectoryMapping>/</outputDirectoryMapping>
</sources>
<binaries>
<outputDirectory>
${module.artifactId}-${module.version}
</outputDirectory>
<dependencySets>
<dependencySet/>
</dependencySets>
</binaries>
</moduleSet>
On running gettting-->Error stacktrace:
org.apache.maven.project.DuplicateProjectException: Project 'sampleMaven:APP2' is duplicated in the reactor
Update: The Maven book has a section on including moduleSets in assemblies. The approach you have in your example is deprecated. There is also a problem with build order when defining moduleSets from the parent. The parent must be built first so the child can inherit from it, but the child must be built so that the parent can includ it in its assembly.
The following approach addresses that cycle.
Define a parent pom that references an assembly module.
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>name.seller.rich</groupId>
<artifactId>test-parent</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
<module>test-assembly</module>
</modules>
<dependencies>
</project>
In the assembly module, define a module with a relative path to the actual application module(s), and define the assembly plugin configuration:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>name.seller.rich</groupId>
<artifactId>test-assembly</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<modules>
<module>../my-app2</module>
</modules>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>App1</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/my-assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
and my-assembly.xml is defined as follows:
<?xml version="1.0" encoding="UTF-8"?><assembly>
<id>my-assembly</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<binaries>
<outputDirectory>
${module.artifactId}-${module.version}
</outputDirectory>
<dependencySets>
<dependencySet/>
</dependencySets>
</binaries>
</moduleSet>
</moduleSets>
</assembly>
Building the parent module will then result in a build order of:
test-parent
my-app2
test-assembly
So when the assembly comes to be packaged, my-app2 is built, and is available for inclusion. The binaries declaration will include the jars.
I am still looking a solution to build a mutli module application, and I think I am almost there !!! :-)
The trick is to build a seperate module and don't add it to parent because you want to build your parent (which builds all modules) and then invoke the assembly.
I got a zip file that contains all i need, only the executable jar doesn't include sources... but I'll try to figure it out asap. If I can make it work, I'll paste the solution here :)
Peace