Maven AspectJ插件非Spring项目无法正常工作 [英] Maven AspectJ plugin non spring project won't work

查看:101
本文介绍了Maven AspectJ插件非Spring项目无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,它不是spring应用程序.我正在尝试在其中使用AspectJ批注. Annotation类是从我拥有的另一个jar中引用的.我在下面提到了POM的插件部分.我的构建成功了,但是Maven的控制台输出从不提及任何有关AspectJ插件的信息,并且在我运行项目时注释不起作用.

I have a project, which is NOT a spring application. I am trying to use AspectJ annotations in it. The Annotation classes are being referenced from another jar I have. I have mentioned my plugin section of POM below. My build succeeds but the console output of Maven never mentions anything about the AspectJ plugin and also the annotations don't work when I run my project.

几个小时以来,我一直在试图找出问题所在,但无法弄清.

I have been trying to find out what's wrong for hours now but can't figure it out.

<pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.4</version>
                <configuration>
                    <complianceLevel>1.8</complianceLevel>
                    <showWeaveInfo>true</showWeaveInfo>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>it.cvc.ciscocommerce.lps.lp-commons</groupId>
                            <artifactId>lp-commons</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <warName>listpriceservice</warName>
                </configuration>
            </plugin>
            <!-- Plugin for sdaas deployment. For compressing war to tar.gz -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>src/main/resources/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.5.201505241946</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </pluginManagement>

这是Jar中定义的两个依赖关系,我正尝试将它们用作方面库.

These are the two dependencies defined in the Jar which I am trying to use as the aspect library.

<dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.7.4</version>
</dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>1.7.4</version>
    </dependency>

这个jar编译良好,我想使用它来开发另一个SPRING应用程序,但不能使用这个应用程序.在SPRING应用程序中,我什至没有定义maven方面插件.

The jar is compiled fine and I amble to use it another SPRING application but not this one. In the SPRING application I don't even have the maven aspect plugin defined.

当我运行Maven构建时,在控制台中,我只列出了以下插件.

When I run the maven build, in console I see only the following plugins listed.

[DEBUG]目标:org.apache.maven.plugins:maven-clean-plugin:2.5:clean(默认为clean) [DEBUG]样式:常规

[DEBUG] Goal: org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) [DEBUG] Style: Regular

[DEBUG]目标:org.apache.maven.plugins:maven-compiler-plugin:3.1:compile(默认编译) [DEBUG]样式:常规

[DEBUG] Goal: org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) [DEBUG] Style: Regular

[DEBUG]目标:org.apache.maven.plugins:maven-resources-plugin:2.6:testResources(默认为testResources) [DEBUG]样式:常规

[DEBUG] Goal: org.apache.maven.plugins:maven-resources-plugin:2.6:testResources (default-testResources) [DEBUG] Style: Regular

[DEBUG]目标:org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile(默认为testCompile) [DEBUG]样式:常规

[DEBUG] Goal: org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) [DEBUG] Style: Regular

[DEBUG]目标:org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test(默认测试) [DEBUG]样式:常规

[DEBUG] Goal: org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) [DEBUG] Style: Regular

[DEBUG]目标:org.apache.maven.plugins:maven-war-plugin:2.4:war(默认为war) [DEBUG]样式:常规

[DEBUG] Goal: org.apache.maven.plugins:maven-war-plugin:2.4:war (default-war) [DEBUG] Style: Regular

编辑:阅读kriegaex的答案以及关于pluginManagement vs插件的信息后,我如下更改了POM.请注意,我的项目不是多模块的,它只有一个POM.

EDIT: After reading kriegaex's answer and about pluginManagement vs plugins, I changed my POM as below. Please note that my project is not multi-module it has only one POM.

<plugins>
        <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.4</version>
                <configuration>
                    <complianceLevel>1.8</complianceLevel>
                    <showWeaveInfo>true</showWeaveInfo>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>it.cvc.ciscocommerce.lps.lp-commons</groupId>
                            <artifactId>lp-commons</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <warName>listpriceservice</warName>
            </configuration>
        </plugin>
        <!-- Plugin for sdaas deployment. For compressing war to tar.gz -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptors>
                    <descriptor>src/main/resources/assembly.xml</descriptor>
                </descriptors>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.5.201505241946</version>
            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

执行此操作时,在AspectJ执行下,执行标签上出现以下错误

When I do this, I get the following error on execution tag under AspectJ executions

生命周期配置未涵盖插件执行:org.codehaus.mojo:aspectj-maven-plugin:1.4:compile(执行:默认,阶段:进程源)

Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:aspectj-maven-plugin:1.4:compile (execution: default, phase: process-sources)

更新我正在解决此问题,因为我原来未调用AspectJ插件的问题已解决.我将在其他问题上打开一个新问题.感谢kriegaex为我指明了正确的方向.

UPDATE I am marking this question as answered as my original issue of AspectJ plugin not being invoked is solved. I will open a new question on my other issue. Thanks to kriegaex for pointing me to the right direction.

推荐答案

我认为这是经典的,完全没有AspectJ问题,只是使用Maven的初学者的错误:

I think this one is a classic and no AspectJ problem at all but a beginners' error using Maven:

您已经在<pluginManagement>部分中定义了插件的默认设置,但是忘记了稍后在单独的<plugins>部分中对其进行引用.因此,Maven根本不知道您要使用它们.

You have defined your plugins' default settings in the <pluginManagement> section but forgot to reference them later in a separate <plugins> section. Thus, Maven has no idea that you want to use them at all.

更新:

好的,我会详细说明一下,因为您似乎仍然无法理解如何使用<pluginManagement><plugins>:您使用前者来定义插件的版本,范围和默认设置.然后,您可以使用后者以便轻松地在您需要的(可能是多模块)项目的任何模块中引用预定义的(托管)插件,而无需复制/粘贴相同的版本和配置.因此,它不是使用这个或那个",而是同时使用它们并以一种聪明的方式将它们组合在一起".示例:

Okay, I will elaborate a bit more as you still seem to have problems understanding how to use <pluginManagement> vs.<plugins>: You use the former in order to define version, scope and default settings for your plugins. Then you use the latter in order to easily just reference the predefined (managed) plugin in whatever module of your (possibly multi-module) project you need them without copying / pasting the same version and configuration anymore. So it is not "use either this or that" but it is "use both and combine them in a smart way". Example:

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>my.group.id</groupId>
            <artifactId>my-plugin-name</artifactId>
            <version>1.2.3</version>
            <configuration>
                <something>foo</something>
            </configuration>
        </plugin>
        <plugin>
            <groupId>my.group.id</groupId>
            <artifactId>my-other-plugin-name</artifactId>
            <version>4.5</version>
            <scope>test</scope>
            <configuration>
                <blah>xyz</blah>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

然后在同一个模块中,或者在另一个模块中,将前一个模块作为父模块或将其导入为物料清单(物料清单):

And then later in the same module or in another module having the former one as a parent or importing it as a BoM (Bill of Materials):

<plugins>
    <plugin>
        <groupId>my.group.id</groupId>
        <artifactId>my-plugin-name</artifactId>
    </plugin>
    <plugin>
        <groupId>my.group.id</groupId>
        <artifactId>my-other-plugin-name</artifactId>
    </plugin>
</plugins>

看到了吗?非常干净和简单.

See? very clean and simple.

顺便说一下,这类似于<dependencyManagement><dependencies>之间的区别.

This is similar to the difference between <dependencyManagement> and <dependencies>, by the way.

您还可以在<plugins>部分中扩展或覆盖托管插件的配置,因此您不仅限于预先配置的内容.

You can also extend or override the configuration for a managed plugin in the <plugins> section, so you are not limited to what was preconfigured.

关于为什么某些托管插件在没有在<plugins>部分中明确提及的情况下起作用的原因:它们是在父POM或Maven根POM中配置的,例如依赖插件,编译插件以及其他预定义和预配置的插件Maven基本插件.如果使Maven显示模块的有效POM,则会看到它们.

As for why some managed plugins worked without you explicitly mentioning them in the <plugins> section: They were configured either in your parent POM or in the Maven root POM, such as the dependency plugin, compile plugin and other predefined and preconfigured Maven base plugins. If you make Maven display the effective POM for your module, you will see them.

AspectJ Maven插件当然不是Maven基本插件,因此您必须自己配置,这是您要尝试做的事情.

The AspectJ Maven plugin is of course not a Maven base plugin, thus you have to configure it by yourself, which is what you are trying to do.

这篇关于Maven AspectJ插件非Spring项目无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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