有没有办法为特定的配置文件运行自定义maven清理?

时间:2016-03-30 09:39:24

标签: java maven jpa build-process pom.xml

所以,我有一个maven配置文件,它为我的JPA实体(ClassName.java)生成我的StaticMetaModel(ClassName_.java)类。

然后,我定义了我的maven-clean-plugin,每次运行mvn clean时都会删除那些生成的类。

<plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <filesets>
            <fileset>
                <directory>${basedir}/src/main/java</directory>
                <includes>
                    <include>**/*_.java</include>
                </includes>
            </fileset>
        <filesets>
    </configuration>
</plugin>

这是生成MetaModel的配置文件。

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>process</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <outputDirectory>src/main/java/</outputDirectory>
                <processors>
                     <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                </processors>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
        <version>${hibernate.version}</version>
        </dependency>
    </dependencies>
</plugin>

一切都按预期工作,但是当我想运行其他配置文件时,我希望maven clean能够正常配置(不删除那些生成的类)。 是否有任何方法可以使用两种方法清洁项目并仅为生成配置文件运行自定义项目?

1 个答案:

答案 0 :(得分:0)

我设法用@Tunaki的消化来解决它。所以谢谢。这是我的解决方案。

在插件部分,我保持maven-clean-plugin不变。

<plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.5</version>
</plugin>

然后,在profiles部分中,我在新的src-gen / main / java文件夹中定义了我的生成配置文件以生成源代码。我需要先删除生成的源代码,所以我为这个配置文件配置了clean插件来删除src-gen文件夹。

<profile>
        <id>hibernate-processor</id>
        <properties>
            <environment.id>hibernate-processor</environment.id>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>2.5</version>
                    <configuration>
                        <filesets>
                            <fileset>
                                <directory>src-gen</directory>
                                <includes>
                                    <include>**/*</include>
                                </includes>
                                <followSymlinks>false</followSymlinks>
                            </fileset>
                        </filesets>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.bsc.maven</groupId>
                    <artifactId>maven-processor-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>process</goal>
                            </goals>
                            <phase>generate-sources</phase>
                            <configuration>
                                <outputDirectory>src-gen/main/java</outputDirectory>
                                <processors>
                                    <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                                </processors>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.hibernate</groupId>
                            <artifactId>hibernate-jpamodelgen</artifactId>
                            <version>${hibernate.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>

之后,我使用build-helper-maven-plugin将src-gen / main / java添加为sources文件夹,如@Tunaki所说。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src-gen/main/java/</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

最后,我只需要配置maven-compiler-plugin来编译src-gen / main / java文件夹中的.java文件。

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <configuration>
            <includes>
                <include>src/main/java/**/*.java</include>
                <include>src-gen/main/java/**/*.java</include>
            </includes>
        </configuration>
    </configuration>
</plugin>