Maven: execute antrun task during package

2020-04-08 00:39发布

I need to use the Maven antrun plugin to add Hibernate bytecode instrumentation to one of my Java classes, in order to enable lazy-loading of individual fields. However, I cannot get the plugin to execute during a build cycle.

How can I instruct Maven to execute the antrun plugin after compilation but before packaging during a mvn package build?

Current pom.xml (snippet):

<pluginManagement>
    <plugins>
    ...
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <inherited>false</inherited>
            <executions>
                <execution>
                    <id>default-cli</id>
                    <configuration>
                        <target>
                            <echo message="Running instrumentation task"/>
                            <taskdef name="instrument" classname="org.hibernate.tool.instrument.javassist.InstrumentTask">
                                <classpath>
                                    <path refid="maven.dependency.classpath" />
                                    <path refid="maven.plugin.classpath" />
                                </classpath>
                            </taskdef>
                            <instrument verbose="true">
                                <fileset dir="target/classes">
                                    <include name="**/UploadedFile.class" />
                                </fileset>
                            </instrument>
                        </target>
                    </configuration>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-core</artifactId>
                    <version>${hibernate.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.javassist</groupId>
                    <artifactId>javassist</artifactId>
                    <version>${javassist.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-simple</artifactId>
                    <version>${org.slf4j-version}</version>
                </dependency>
            </dependencies>
        </plugin>
    ...
    </plugins>
</pluginManagement>

All of the documentation I have seen regarding this issue shows the plugin being configured to run during the "process-classes" phase. However, from the Maven docs, it doesn't appear that the "process-classes" phase is part of the build cycle for package. I can run the plugin on its own using mvn antrun:run, but since in the end I need to do mvn package to produce a .war file for deployment, I am fairly certain that this plugin needs to execute somewhere within the package task in order to place the modified class into the packaged .war.

I have tried many variations of the above code (changing the phase, changing with , updating the plugin's version, changing the id, etc), with no perceivable changes. I have been running mvn with -e -X to display all possible output, and the antrun plugin is never executed no matter what I try. Any help would be greatly appreciated.

1条回答
虎瘦雄心在
2楼-- · 2020-04-08 01:46

Apparently, the problem is in the fact that your antrun plugin configuration is located in pluginManagement section instead of plugins section. You can see the difference between these 2 sections in my answer here.

To make it work you should move this to <build> -> <plugins> section of your pom.xml.

查看更多
登录 后发表回答