2

我有以下 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>

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

我究竟做错了什么?

4

1 回答 1

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 开头 - 它是来自根 / 的全局路径。对你来说,根是项目 ${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 中更新 Override 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>
于 2013-10-30T08:48:47.100 回答