maven-clean-plugin不删除文件

时间:2013-10-30 08:27:34

标签: java maven plugins

我有以下pom.xml,我想在generated阶段放入*.java个文件之后删除generate-sources目录:

    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <type>maven-plugin</type>
    </dependency>
</dependencies>
<build>
    <plugins>
        <!-- This plugin generates java files in generated directory -->
        <plugin>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro-maven-plugin</artifactId>
            <version>${avro.version}</version>
            <executions>
                ...
            </executions>
        </plugin> 
        <!-- To clean the generated directory in service package -->           
        <plugin>
            <groupId>maven</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <filesets>
                    <fileset>
                        <directory>/src/main/java/com/acme/Network/service/generated</directory>
                        <includes>
                            <include>**/*.java</include>
                        </includes>
                        <excludes>
                            <exclude>**/*.log</exclude>
                        </excludes>  
                        <followSymlinks>false</followSymlinks>
                    </fileset>
                </filesets>
            </configuration>
        </plugin>
    </plugins>
</build>

我希望从m2e中使用maven clean删除整个包。但它只删除目标目录,生成的目录保持不变。

我做错了什么?

1 个答案:

答案 0 :(得分:10)

首先 - 从依赖项中删除maven-clean-plugin。它是插件,而不是依赖。

第二次:尝试

<plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>${basedir}/src/main/java/com/acme/Network/service/generated</directory>
                        </fileset>
                    </filesets>
                </configuration>

            </plugin>

您的路径以/ src开头 - 它是来自root /的全局路径。对你来说,root是项目$ {basedir}

<强>第三 应按惯例将生成的源添加到:

${basedir}/target/generated-sources 然后,当你运行“干净” - 它也将被删除。您可以跳过第二步步骤。

使用helper插件将其他源包添加到项目中(intellij和eclipse应该看到它):

     <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>test</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>add-source</goal>
                            </goals>
                            <configuration>
                                <sources>
                                    <source>${basedir}/target/generated-sources</source>
                                </sources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

<强>更新 在你的pom中覆盖avro-maven-plugin

<plugin>
  <groupId>org.apache.avro</groupId>
  <artifactId>avro-maven-plugin</artifactId>
  <version>${avro.version}</version>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>schema</goal>
      </goals>
      <configuration>
        <sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
        <outputDirectory>${project.basedir}/target/generated-sources/</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>