【问题标题】:How to delete a file while doing maven install (build) from target directory如何在从目标目录执行 Maven 安装(构建)时删除文件
【发布时间】:2019-02-09 21:00:45
【问题描述】:

我希望从目标文件夹中删除一个文件夹 (api-docs)。当我进行 Maven 构建时,生成目标文件夹时,它应该排除该文件夹(api-docs)。
目标包含类、codegen、生成源、javadoc-bundle-options、maven-archiver、maven-status、test-classes 和一个 war 文件。
我需要排除 codegen 中存在的(api-docs)
Codegen > generated-sources> web> api-docs( 包含 css、字体、图像、lang、lib、specs 和其他一些 js 和 html文件)

 <plugin>
                         <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.8</version>
                        <executions>
                            <execution>
                                <phase>build</phase>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                                <configuration>                                  
                                    <tasks>
                                        <delete>
                                          <fileset> dir="${project.build.outputDirectory}/target/codegen/generated-sources/web/api-docs"/>
                                        </delete>
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>  
          `


我在 pom.xml 中添加了该内容,但无法删除。请建议
这是从 pom 文件构建的全部内容

<build> 
<pluginManagement>
<plugins>
<plugin>
<groupId>com.xxx.chassis.api.plugins</groupId>
<artifactId>codegen-maven-plugin</artifactId>
<version>${codegen.plugin.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<templateDir>chassis-archetypes</templateDir>
<configFile>${project.parent.basedir}/codegen-config.yaml</configFile>
<specifications>
<specification>${project.parent.basedir}/partnerships-originations-product-offer-id.yaml</specification>
</specifications>
<basePackage>com.xxx.papi.popoi</basePackage>
</configuration>
</plugin> 

           <plugin>
                     <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                        <execution>
                            <phase>build</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>                                  
                                <tasks>
                                    <delete>
                                      <fileset dir="${project.build.directory}/codegen/generated-sources/web/api-docs/swagger-ui.min.js"/>
                                    </delete>
                                </tasks>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>  

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${codegen.generated-sources}/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<sourcepath>${codegen.generated-sources}/java</sourcepath>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

【问题讨论】:

  • 如果你需要使用build-helper-maven-plugin插件codegen-maven-plugin 是错误实现的......

标签: java maven


【解决方案1】:

请分享您的 pom 文件的 build->plugins 部分的全部内容。不必将 maven-antrun-plugin 添加到您的 pom.xml 中。目标目录中的 apidocs 文件夹通常由您的 pom 文件中可能存在的 maven-javadoc-plugin 生成。防止创建 apidocs 文件夹的第一种方法是从您的 pom.xml 中删除该插件声明。另一种方法是根据此答案提供 maven.javadoc.skip 参数:https://stackoverflow.com/a/9360317/7810853

【讨论】:

    【解决方案2】:

    antrun-plugin 的目标是 run,而不是 build。相应地更改 pom.xml 应该会有所帮助:

    [...]
    <goals>
      <goal>run</goal>
    </goals>
    [...]
    

    您还应该仔细检查 api-doc 文件夹是在哪个phase 创建的,并且您应该在稍后阶段调用 ant-plugin,例如package

    [...]
    <phase>package</phase>
    [...]
    

    第三点,Maven 变量${project.build.outputDirectory} 通常引用文件夹target/classes。详情请查看this answer。因此,您可以使用

    <fileset dir="${project.basedir}/target/codegen/generated-sources/web/api-docs"/>
    

    或者——更准确地说——

    <fileset dir="${project.build.directory}/codegen/generated-sources/web/api-docs"/>
    

    【讨论】:

    • 在上面的代码中,我已经将目标运行和文件集更改为 dir="${project.build.directory}/codegen/generated-sources/web/api-docs/swagger-ui. min.js"/>
      仍然无法删除文件夹/文件 swagger-ui.min.js
    • 能否请您检查一下是否调用了ant插件?也许您可以在运行构建时发布最新版本的 pom.xml 和完整输出(例如在 pastebin 上)?
    猜你喜欢
    • 2016-08-31
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 2010-12-11
    相关资源
    最近更新 更多