Maven:在包装期间执行antrun任务 [英] Maven: execute antrun task during package

查看:171
本文介绍了Maven:在包装期间执行antrun任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Maven antrun插件将Hibernate字节码工具添加到其中一个Java类中,以启用个别字段的延迟加载。但是,我无法让插件在构建周期中执行。



如何指示Maven在编译后但在打包前执行antrun插件 mvn package build?



当前pom.xml(片段):

 < pluginManagement> 
< plugins>
...
< plugin>
< artifactId> maven-antrun-plugin< / artifactId>
< version> 1.7< / version>
<继承>假< /继承>
<执行次数>
<执行>
< id> default-cli< / id>
<配置>
< target>
< echo message =运行检测任务/>
< taskdef name =instrumentclassname =org.hibernate.tool.instrument.javassist.InstrumentTask>
< classpath>
< path refid =maven.dependency.classpath/>
<路径refid =maven.plugin.classpath/>
< / classpath>
< / taskdef>
< instrument verbose =true>
< fileset dir =target / classes>
< include name =** / UploadedFile.class/>
< / fileset>
< / instrument>
< / target>
< / configuration>
<阶段>程序类< /阶段>
<目标>
< goal>跑步< / goal>
< /目标>
< /执行>
< /执行次数>
<依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> $ {hibernate.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.javassist< / groupId>
< artifactId> javassist< / artifactId>
< version> $ {javassist.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.slf4j< / groupId>
< artifactId> slf4j-simple< / artifactId>
< version> $ {org.slf4j-version}< / version>
< /依赖关系>
< /依赖关系>
< / plugin>
...
< / plugins>
< / pluginManagement>

所有关于此问题的文档都显示插件被配置为在process类阶段。但是,从Maven 文档,它不会看起来流程类阶段是 package 的构建周期的一部分。我可以使用 mvn antrun:run 自行运行插件,但由于最终我需要执行 mvn package 生成一个用于部署的.war文件,我相当肯定这个插件需要在包任务内的某个地方执行,以便将修改后的类放入打包的.war文件中。

我已经尝试了上面代码的许多变体(更改阶段,更改插件版本,更改ID等),但没有可察觉的更改。我一直使用 -e -X 运行 mvn 来显示所有可能的输出,并且antrun插件永远不会执行no不管我尝试什么。任何帮助将不胜感激。

解决方案

显然,问题在于您的 antrun 插件配置位于 pluginManagement 部分,而不是插件部分。你可以在我的回答中看到这两部分之间的区别这里



为了使它工作,您应该将它移动到< build> - > < plugins> 您的pom.xml部分。


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.

解决方案

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.

这篇关于Maven:在包装期间执行antrun任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆