-->

No source jar attached with maven-assembly-plugin

2019-04-13 06:33发布

问题:

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?

回答1:

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>


回答2:

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}