问题
I have a maven project with quite a few submodules. What I am looking for is a way to get all the .jar files produced by the sub-modules included in the aggregating POM's /target/ directory, so they can be conveniently used afterwards.
- They don't need to be merged. Preferably not, but if they must be then that is ok.
- Don't care about dependancies
- This is primarily for convenience, at this point
A basic version of what I am looking at doing:
Prj1/pom.xml => prj1/target/proj1.jar (and classes/generated-sources/etc)
Prj2/pom.xml => prj2/target/proj2.jar
Main/pom.xml =>
main/target/proj1.jar
main/target/proj2.jar
... classes/generated-sources not needed at all,
... but they could be combined here. I assume they will be
I've been reading, and using some suggestions from SO as well. So far I haven't found a way to do this, but I'm sure it is there.
edit:
I've given up on getting this to work in a simple way, for all included subprojets. The best answer I have so far is using the dependancy plugin to manually specify (again), each and every sub-module to be included. The idea was to be able to configure the POMs easily for the dozens of clients, simply including the modules necessary and then having it magically stick all the sub-modules's jars in one location. Maven is pretty nice, when you don't do much with it, but the angle bracket tax is incredible when you try.
I still find it odd that such standard-seeming tasks (judging from the questions asked on SO, and what any normal project would do) are so difficult. Is maven3 better?
回答1:
You could try the maven-dependency-plugin:copy plugin:goal.
You will have to add this to the pom of all submodules that you want to copy. EDIT: Or just in the parent pom (see comments).
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-artifact</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<type>${project.packaging}</type>
</artifactItem>
</artifactItems>
<outputDirectory>../Main/target/dependencies</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
If you do put this in the parent pom, keep in mind that it will work nicely only if your whole project is one module deep. Meaning that if the the modules under the parent module have their own submodules, they will not end up in the desired folder. Your folder structure will look like this:
- Parent
- Module 1
- Sub module 1
**Main**
- Module 2
**Main**
To prevent this, create a dedicated module with above configuration, and specify manually each module that you want to copy. This way all modules, no matter how deep they are will end up in one folder.
回答2:
I have spent the entire day trying to solve this... and finally it works, so even though I am on a tough schedule, I will share it here, if I can only save someone's frustration in the future...
Step 1. Create an additional Maven project that will be a parent of all the projects that you want to copy together. Let's call this project parent
.
This project is needed to tell Maven, which projects to build together. Also, you will declare in your other projects that their parent is parent
, so they will see the MyDir
property that you define here.
<groupId>com.your.domain</groupId>
<artifactId>parent</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<properties>
<MyDir>/your/path/to/copy/to</MyDir>
</properties>
<modules>
<module>../project1</module>
<module>../project2</module>
<module>../project2</module>
</modules>
Step 2. For every project that you want to be copied to the same location, specify that it's parent is the parent
project (make sure you specify correct groupId and artifactId and version of your parent project):
<parent>
<groupId>com.your.domain</groupId>
<artifactId>parent</artifactId>
<version>0.0.1</version>
<relativePath>../parent</relativePath>
</parent>
And also, for each of these projects, also specify the jar and dependency plugins settings, like so:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<outputDirectory>${MyDir}</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.your.domain.Program</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${MyDir}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Then just run mvn install
on the parent project. Bam!
P.S. The above assumes all projects are located in the same directory (parent project next to children), but you can change relative paths as you wish.
回答3:
One way to achieve this would be to use the moduleSet option of maven assembly plugin.
You could create an assembly descriptor like this (a variation of the example in the link) and use it in assembly plugin declaration in the pom.
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>bin</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<binaries>
<outputDirectory>/</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
</moduleSets>
</assembly>
来源:https://stackoverflow.com/questions/8292752/maven-multi-module-project-copying-all-package-jars-from-submodules-into-par