【问题标题】:Maven package only includes file in second runMaven 包仅在第二次运行时包含文件
【发布时间】:2016-04-20 10:27:13
【问题描述】:

我正在创建一个非 java Maven 工件。在pom.xml 中,我解决了一些依赖关系,然后使用exec 插件运行自定义脚本。有些文件是在目录中创建的,但是 Maven 在将它们打包到 jar 中时看不到它们。

当我运行mvn package 两次时,第二次运行确实包含 jar 中的资源文件。

任何想法为什么会发生这种情况?该脚本在compile 阶段运行,因此在package 阶段创建jar 时文件已经创建。


这是我的pom.xml 配置的相关(希望)部分:

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.4.0</version>
        <executions>
            <execution>
                <id>build-plugin</id>
                <phase>compile</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
                <configuration>
                    <executable>bash</executable>
                    <arguments>
                        <argument>build_plugin.sh</argument>
                        <argument>${workspace}</argument>
                        <argument>${plugin}</argument>
                    </arguments>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

<resources>
    <resource>
        <directory>${project.basedir}/${outputPath}</directory>
        <includes>
            <include>**</include>
        </includes>
        <excludes>
            <exclude>target/**</exclude>
        </excludes>
    </resource>
</resources>

所有变量和路径都是有效的,在第二次运行时,我得到了一个包含预期内容的 jar。但不是在第一次。

【问题讨论】:

    标签: java maven maven-resources-plugin


    【解决方案1】:

    在 maven 默认生命周期中,资源的处理发生在编译之前 见https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

    你要做的是改变“exec-maven-plugin”的构建阶段,使用“generate-sources”而不是“compile”

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.4.0</version>
            <executions>
                <execution>
                    <id>build-plugin</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>bash</executable>
                        <arguments>
                            <argument>build_plugin.sh</argument>
                            <argument>${workspace}</argument>
                            <argument>${plugin}</argument>
                        </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    
    <resources>
        <resource>
            <directory>${project.basedir}/${outputPath}</directory>
            <includes>
                <include>**</include>
            </includes>
            <excludes>
                <exclude>target/**</exclude>
            </excludes>
        </resource>
    </resources>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 2019-09-18
      相关资源
      最近更新 更多