4

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.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Amanda_A
  • 323
  • 1
  • 3
  • 10
  • In what section (`plugins` or `pluginManagement`) is this configuration located? – Andrew Logvinov Dec 19 '12 at 20:03
  • It's in `pluginManagement`. I'll add that to the pom snippet in the post. – Amanda_A Dec 19 '12 at 20:19
  • That's the reason. It never gets executed. See my answer [here](http://stackoverflow.com/questions/13959358/where-should-be-placed-maven-compiler-plugin-declaration-in-plugins-or-plugi/13959711#13959711) to understand the difference between these sections. You should move it to ` -> `. – Andrew Logvinov Dec 19 '12 at 20:25
  • Interesting, thanks for the explanation. I originally had it in `` without the outer `` tag, but my IDE complained (Eclipse, "Plugin execution not covered by lifecycle configuration") and I found another answer (I've lost the link) that indicated the problem would be solved by adding the `pluginManagement` outer tag. I just tried running `mvn package` from the commandline after removing the tag, and that executes the task correctly. My IDE still complains but that's OK as long as it works. Thanks! If you leave an answer I'll mark it as correct. – Amanda_A Dec 19 '12 at 20:35
  • You're welcome =) I posted the answer. – Andrew Logvinov Dec 19 '12 at 20:39

1 Answers1

7

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.

Community
  • 1
  • 1
Andrew Logvinov
  • 21,181
  • 6
  • 52
  • 54