4

I'm new to Maven, and I think I've started to get the idea of how it works. But I'm not able to understand the maven assembly plugin. What I want to achieve is this:

When all projects has been packaged, with their respective dependencies, I want to have them all in the target directory. I don't want them to be packaged into one super-jar because the system is based on modules.

Let me explain, I have a main project, the server, in the maven project "common", and I have two modules, "core" and "android". In the common folder, there is also a conf folder that I want copied over. I want this structure:

  • target/common.jar
  • target/conf/(configuration files)
  • target/modules/core.jar
  • target/modules/android.jar

My project structure is this:

  • pom.xml (parent project)
  • common/ (maven module)
  • core/ (maven module)
  • android/ (maven module)

Thank you for any help, or pointers in the right way. :)

EDIT Here is the ant build file which works 100%, maybe I should keep with that?

<target name="init">
    <mkdir dir="dist" />
    <mkdir dir="dist/conf/" />
    <mkdir dir="dist/modules/" />
    <mkdir dir="dist/libs/" />

    <copy includeemptydirs="false" todir="dist/conf">
        <fileset dir="common/conf" />
    </copy>
</target>

<target name="copy-server">
    <copy todir="dist">
        <fileset file="common/target/server*.jar" />
    </copy>
</target>

<target name="copy-modules">
    <copy todir="dist/modules/">
        <fileset file="core/target/*.jar" />
        <fileset file="android/target/*.jar" />
    </copy>
</target>

<target name="copy-libs">
    <copy todir="dist/libs">
        <fileset dir="common/target/libs" />
        <fileset dir="core/target/libs" />
        <fileset dir="android/target/libs" />
    </copy>
    <delete>
        <fileset file="dist/libs/server*.jar" />
    </delete>
</target>

<target name="clean">
    <delete dir="dist" />
</target>

<target name="full-build" depends="clean, init, copy-server, copy-libs, copy-modules, increment">
    <echo message="Copying the fully built Maven project" />
</target>

<target name="increment">
    <propertyfile file="common/conf/version.properties">
        <entry key="build.number" type="int" operation="+" default="0" />
    </propertyfile>
    <property file="common/conf/version.properties" />
    <echo message="Build number is ${build.number}"/>
</target>

Aldrian
  • 2,919
  • 1
  • 17
  • 11
  • `target` is just a working directory where builds are performed. You shouldn't care what that looks like. I think what you're wanting is to build a distributable archive with the assembly plugin that has the appropriate jars and config files in it. Is that right? – Ryan Stewart May 28 '12 at 19:47
  • Ryan, that does indeed sound correct. The question is how to achieve the effect I want. Got some pointers I could follow for, like, getting the jars into the folders I want, and copy the folder? – Aldrian May 28 '12 at 22:37
  • @Aldrian - If you know how to do this, I have a question here: http://stackoverflow.com/q/33088454/1735836 – Patricia Oct 12 '15 at 19:44

3 Answers3

4

First, what the assembly plugin does: it makes it really easy to make a tar or zip archive containing a Maven project's artifact(s), dependencies, and other related files.

It sounds like all you need to do is set up the assembly plugin with a custom descriptor to pull in the jars and config files you're interested in. If you have a single module that represents a "distributable" thing--even if that module depends on other modules--then you'd probably just add an assembly descriptor to it that includes some files, fileSets, and/or dependencySets.

If there are several modules in your project that you want to include and there's nothing that depends on all of them, then you'd want to look at moduleSets. I've never had to use those myself, but they're for exactly that problem.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • Ryan - If you know how to do this, I have a question here: http://stackoverflow.com/q/33088454/1735836 – Patricia Oct 12 '15 at 19:44
3

This setup will solve it exactly as you want it.

directory layout

+- pom.xml
+- android
| +- pom.xml
| +- src
|   +- main
|     +- java
+- core
| +- pom.xml
| +- src
|   +- main
|     +- java
+- common
  +- pom.xml
  +- src
    +- main
     +- java
     +- resources
       +- conf

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>com.example</groupId>
    <artifactId>my-project</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <modules>
        <module>android</module>
        <module>common</module>
        <module>core</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>core</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>android</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

android/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>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>android</artifactId>
    <packaging>jar</packaging>

    <name>${project.artifactId}-${project.version}</name>

</project>

core/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>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>core</artifactId>
    <packaging>jar</packaging>

    <name>${project.artifactId}-${project.version}</name>

</project>

common/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>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>common</artifactId>
    <packaging>jar</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>android</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>${project.build.directory}</targetPath>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/modules</outputDirectory>
                            <stripVersion>true</stripVersion>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

This last pom is where you can change behavior according to your needs.

If you want to pack all this in some uber-jar then you are free to do so.


Edit:

Ok, after reading your comments and looking at your Ant build script I have come up to the following design/setup that would give you more or less what you want.

directory layout

+- pom.xml
+- android
| +- pom.xml
| +- src
|   +- main
|     +- java
+- core
| +- pom.xml
| +- src
|   +- main
|     +- java
+- common
| +- pom.xml
| +- conf.xml
| +- src
|   +- main
|    +- java
|    +- resources
|      +- conf
+- dist
  +- pom.xml

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>com.example</groupId>
    <artifactId>my-project</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <modules>
        <module>android</module>
        <module>common</module>
        <module>core</module>
        <module>dist</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>common</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>common</artifactId>
                <version>${project.version}</version>
                <classifier>conf</classifier>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>core</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>android</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

android/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>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>android</artifactId>
    <packaging>jar</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
        </dependency>
    </dependencies>
</project>

core/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>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>core</artifactId>
    <packaging>jar</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
        </dependency>
    </dependencies>
</project>

common/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>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>common</artifactId>
    <packaging>jar</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <id>assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>conf.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

common/conf.xml

This assembly descriptor will package your conf files in a separate jar and any project will be able to have a dependency on it.

<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>conf</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${basedir}/src/main/resources/conf</directory>
            <outputDirectory>conf</outputDirectory>
            <includes>
                <include>*.properties</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

dist/pom.xml

The dist module will unpack the conf dependency and copy the other dependencies to your target directory.

<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>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>dist</artifactId>
    <packaging>pom</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
            <classifier>conf</classifier>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>android</artifactId>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>core</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>modules</id>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.example</groupId>
                                    <artifactId>android</artifactId>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.example</groupId>
                                    <artifactId>core</artifactId>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.example</groupId>
                                    <artifactId>common</artifactId>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${project.build.directory}/modules</outputDirectory>
                            <stripVersion>true</stripVersion>
                        </configuration>
                    </execution>
                    <execution>
                        <id>unpack</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.example</groupId>
                                    <artifactId>common</artifactId>
                                    <classifier>conf</classifier>
                                </artifactItem>
                            </artifactItems>
                            <excludes>**/MANIFEST.MF</excludes>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

I tend to like working with the maven-dependency-plugin and I find it quite powerful.

The dist module will have all the necessary dependencies to the other modules and download and unpack as you wish. If you then would like to have it all packed in a zip or tar.gz or some other format then you could use the maven-assembly-plugin for that.

maba
  • 47,113
  • 10
  • 108
  • 118
  • First of all, thank you for your help! While closer, this unfortionally didn't solve the case. When I added dependencies in the parent project, the project was complaining because of cyclic dependencies. Core and Android both depends on common. And I have most of the dependencies defined in the main project pom. – Aldrian May 29 '12 at 11:54
  • @Aldrian You want `android.jar` and `core.jar` to end up in `common/target` but `common.jar` has dependencies to both `android.jar` and `core.jar`? OK, then you have a problem. Having dependencies defined in the main `pom.xml` is normally not a problem though. I think that you have to re-think about your model. An alternative would be to have a fourth module called e.g. `deployment` which will resolve all the other dependencies. – maba May 29 '12 at 12:20
  • Well, common.jar doesn't depend on android.jar and core.jar, but android.jar and core.jar depends on common.jar , yeah I think I'll need a fourth model, I have a temporarily solution now with a Ant build script, going to post that as it might help figuring out how to compleatly do this in maven. – Aldrian May 29 '12 at 13:39
0

Maven will default to the following structure when building for your project:

  • common/target/common-.jar
  • core/target/core-.jar
  • android/target/android-.jar

However, all is not lost! As with most options in maven, this can easily be overwritten using the proper configuration tags. In your case, you want to set the outputDirectory tag under your tag (in each of the module POMS) to the proper directory.

For example, your ${basedir}/common/pom.xml should be edited to add this line:

...
<build>
    <outputDirectory>../target</outputDirectory>
...


Your ./android/pom.xml would be edited similarly:

...
<build>
    <outputDirectory>../target/modules</outputDirectory>
...

I'm not sure the best way to copy over the configuration files. You may want to try to find (or write, since it is a fairly simple operation) a plugin to simply copy files over and give it a scope such as "compile" while pointing it at the proper directory.

matt5784
  • 3,065
  • 2
  • 24
  • 42
  • Thanks, but that unfortionally didn't do what I expected to. By setting the build output directory, the only thing that happened was that my compiled class files were moved, as well as resources. If I execute maven package, the jars would still be in the target folder for the respective module :/ – Aldrian May 28 '12 at 21:59
  • You're right, of course. I think the outputDirectory tag needs to be under the tag inside the maven-compiler-plugin, a la org.apache.maven.plugins maven-compiler-plugin 2.5 ../target – matt5784 May 29 '12 at 14:41