3

I have the following project maven (3.10) multi project:

my-mvn
 -> my-mvn-a
 -> my-mvn-b
 -> my-mvn-assembly

my-mvn-a and my-mvn-b build source jars using:

<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>my.group</groupId>
        <artifactId>my-mvn</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <artifactId>my-mvn-a</artifactId>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

In my-mvn-assembly I would like to build a zip including jars and source jars from my-mvn-a and my-mvn-b. The pom.xml file:

<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>my.group</groupId>
        <artifactId>my-mvn</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <artifactId>my-mvn-assembly</artifactId>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <finalName>${project.artifactId}-${project.version}</finalName>
                    <descriptors>
                        <descriptor>src/main/resources/my_assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>my.group</groupId>
            <artifactId>my-mvn-a</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

And the descriptor:

<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>my-assembly</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <includes>
                <include>my.group:my-mvn-a:jar:${project.version}</include>
                <include>my.group:my-mvn-a:jar:sources:${project.version}</include>
            </includes>
        </dependencySet>
    </dependencySets>
</assembly>

but only the jar with the compiled classes is included in the zip. Why does it not include the sources jar build using the maven-sources-plugin?

u123
  • 15,603
  • 58
  • 186
  • 303

2 Answers2

4

It seems to me that the phase in which you're generating the sources is too late. The verify phase comes after the package phase which means that your sources jar is getting generated, but it's well after the packaging of the assembly has occurred. Set the phase of the maven-sources-plugin to package and make sure the maven-assembly-plugin is defined in the same phase but declared after it (as Maven invokes the plugins in consecutive order).

EDIT 1:

I figured it out. You haven't defined a dependency to the sources artifact. Add the following and it will work:

<dependency>
    <groupId>my.group</groupId>
    <artifactId>my-mvn-a</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <classifier>sources</classifier>
</dependency>
carlspring
  • 31,231
  • 29
  • 115
  • 197
  • You could further improve this by marking all your sources dependencies with scope `provided` and adding an invocation of the maven-dependency-plugin that executes the copy-dependency goal and copies them to a location under the target directory and then re-work your assembly descriptor to include those artifacts from there instead. This will make Maven not include the sources artifacts in wars/zips etc. – carlspring Sep 02 '13 at 13:37
  • Yes adding it as a dependency was thing I was missing! Actually I am now using the maven-dependency instead in a similar way as you describe. – u123 Sep 02 '13 at 14:02
  • The way I described is the better way of doing it, as otherwise, if these dependencies aren't with provided scope, different plugins will include then in their operations and you clearly don't need the sources jars at those times. I would also like to recommend that you post more complete `pom.xml`-s in the future (such as the ones you added now) as this will help more people understand what your actual problem is. All the best! :) – carlspring Sep 02 '13 at 14:10
  • So -- do I get the bounty? ;) – carlspring Sep 02 '13 at 14:10
  • Thanks for the input/help, I have assigned the bounty to your answer, leave a message if it does not work. – u123 Sep 02 '13 at 15:14
0

I guess it is because you didn't include the version in the <include>.

According the documentation, you can either use the short form:

<include>groupId:artifactId</include>

either the fully qualified form

<include>groupId:artifactId:type[:classifier]:version</include>

So try this:

<include>my.group:my-mvn-a:jar:sources:*</include>

or this

<include>my.group:my-mvn-a:jar:sources:1.0-SNAPHOT</include>

(using your actual version number of course, or better a property like ${my-mvn-a.version}

ben75
  • 29,217
  • 10
  • 88
  • 134
  • Adding version has no effect, the zip archive still only contains the jar with the compiled classes. – u123 Sep 02 '13 at 09:23