没有使用maven-assembly-plugin附加的源jar

时间:2013-08-27 22:08:53

标签: maven maven-assembly-plugin

我有以下项目maven(3.10)多项目:

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

my-mvn-amy-mvn-b使用以下内容构建源代码库:

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

my-mvn-assembly中,我想构建一个包含来自my-mvn-amy-mvn-b的jar和来源jar的zip。 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>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>

描述符:

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

但是只有带有编译类的jar包含在zip中。为什么它不包含使用maven-sources-plugin构建的jar源?

2 个答案:

答案 0 :(得分:3)

在我看来,您生成源的阶段为时已晚。 verify阶段之后的package阶段意味着您的源jar正在生成,但是在程序集打包之后它很好。将maven-sources-plugin的阶段设置为package并确保maven-assembly-plugin在同一阶段定义,但在其后声明(因为Maven按连续顺序调用插件)。

编辑1:

我明白了。您尚未定义对sources工件的依赖关系。添加以下内容,它将起作用:

<dependency>
    <groupId>my.group</groupId>
    <artifactId>my-mvn-a</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <classifier>sources</classifier>
</dependency>

答案 1 :(得分:0)

我想这是因为你没有在<include>中包含该版本。

根据documentation,您可以使用简短形式:

<include>groupId:artifactId</include>

完全合格的表格

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

所以试试这个:

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

或者

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

(使用您当前的实际版本号,或更好的属性,如${my-mvn-a.version}