3

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

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

my-mvn-amy-mvn-b使用以下方法构建源 jar:

<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我想构建一个包含 jar 和源 jar 的 zipmy-mvn-amy-mvn-b. 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?

4

2 回答 2

4

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

编辑1:

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

<dependency>
    <groupId>my.group</groupId>
    <artifactId>my-mvn-a</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <classifier>sources</classifier>
</dependency>
于 2013-09-02T10:59:10.363 回答
0

我想这是因为你没有在<include>.

根据文档,您可以使用简写形式:

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

于 2013-08-30T13:34:55.140 回答