How to delete a file while doing maven install (build) from target directory

Viewed 1975

I am looking to delete a folder (api-docs) from the target folder. When I do maven build, when the target folder is generated, it should exclude that folder(api-docs).
Target contains Classes, codegen, generated-sources, javadoc-bundle-options, maven-archiver, maven-status, test-classes and a war file.
I need to exclude(api-docs) which is present in codegen
Codegen > generated-sources> web> api-docs( contains css, fonts, images, lang, lib, specs and some other js and html files)

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


I added that in pom.xml, but couldn't able to delete. Please suggest
Here is the entire contents of build from pom file

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

Please share the entire contents of the build->plugins section of your pom file. It should not be necessary to add the maven-antrun-plugin to your pom. The apidocs folder in your target directory is usually produced by the maven-javadoc-plugin which you possibly have present in your pom file. The first way to prevent the creation of the apidocs folder would be to remove that plugin declaration from your pom. An alternative way would be to supply the maven.javadoc.skip argument as per this answer: https://stackoverflow.com/a/9360317/7810853

The goal for the antrun-plugin is run, not build. Changing the pom.xml accordingly should help:

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

You also should double check in which phase the api-doc folder is created and you should call the ant-plugin in a later phase, for example package:

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

As a third point, the Maven variable ${project.build.outputDirectory} usually references the folder target/classes. Please check this answer for more details. Therfore, you could either use

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

or - more precise -

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