maven-clean-plugin不删除文件

mCs*_*mCs 2 java plugins maven

我有以下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>
Run Code Online (Sandbox Code Playgroud)

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

我究竟做错了什么?

And*_*wik 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>
Run Code Online (Sandbox Code Playgroud)

你的路径以/ 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>
Run Code Online (Sandbox Code Playgroud)

在你的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>
Run Code Online (Sandbox Code Playgroud)

  • +1令人遗憾的是,Avro文档诱使人们在`src/main /`文件夹中生成代码,这非常危险. (2认同)