【问题标题】:"Deleting" the generated default jar in Maven在 Maven 中“删除”生成的默认 jar
【发布时间】:2018-08-30 04:29:58
【问题描述】:

有一些解决方案可以“禁用”maven-jar-plugin 生成的默认 jar。但是,由于我的最终工件可能依赖于这个 jar(我对 maven 比较陌生,所以不完全确定是否确实如此,但似乎如此)当我禁用它时,我的构建失败并出现以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.3:shade (build-cli) on project putwb: Failed to create shaded artifact, project main artifact does not exist. -> [Help 1]

否则,它会生成一个空 jar 以及我通过脚本构建的其他 jar。构建完成后,有人可以建议一种“删除”默认 jar 的方法吗?或者,有人可以指出我如何禁用它,但我的构建成功了吗?

我在下面发布了我的pom.xml 的相关部分:

    <build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
                <execution>
                    <id>readme-md</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.basedir}</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <includes>
                                    <include>README.md</include>
                                </includes>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <!-- Build the CLI version -->
                    <phase>package</phase>
                    <id>build-cli</id>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <!-- Don't include the UI code -->
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>in/ac/iitk/cse/putwb/ui/**</excludes>
                            </filter>
                        </filters>
                        <finalName>putwb-cli-${project.version}</finalName>
                        <transformers>
                            <!-- Don't include images -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>.png</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>in.ac.iitk.cse.putwb.experiment.PUTExperiment</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
                <execution>
                    <!-- Build the UI version -->
                    <phase>package</phase>
                    <id>build-ui</id>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                    <finalName>putwb-ui-${project.version}</finalName>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>in.ac.iitk.cse.putwb.ui.PUTWb</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- If I add this to the script, the build fails -->
        <!--
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
                <execution>
                    <id>default-jar</id>
                    <phase>none</phase>
                </execution>
            </executions>
        </plugin>
        -->
    </plugins>
  </build>

【问题讨论】:

    标签: java maven maven-jar-plugin


    【解决方案1】:

    您可以使用 ant-run 插件的删除任务。您可以在生命周期中晚于 package 阶段的任何阶段执行此插件。(因为它将在 package 阶段再次创建。检查哪个套件更适合您。)执行您选择作为此插件阶段的 maven 命令(如mvn verify 如果你选择验证阶段)

            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-antrun-plugin</artifactId>
              <version>1.8</version>
              <executions>
                  <execution>
                      <phase>package</phase>
                      <goals>
                          <goal>run</goal>
                      </goals>
                      <configuration>
                          <target>
                              <delete file="${project.build.directory}/YourJarName.jar"/>
                          </target>
                      </configuration>
                  </execution>
              </executions>
        </plugin>
    

    检查构建生命周期https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

    感谢@markthegrea 指出正确的文件夹。

    【讨论】:

    • 这对我有用,但您必须将 ${project.build.outputDirectory} 更改为 ${project.build.directory},outputDirectory 是“目标/类”。见cwiki.apache.org/confluence/display/MAVEN/…。找到并删除文件时有日志。
    猜你喜欢
    • 2012-09-30
    • 2016-06-24
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 2019-04-18
    • 2018-09-13
    相关资源
    最近更新 更多