Maven AspectJ plugin non spring project won't work

主宰稳场 提交于 2019-12-11 18:49:36

问题


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>

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>

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.

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

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

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

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

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

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

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

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>

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

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

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.


回答1:


I think this one is a classic and no AspectJ problem at all but a beginners' error using 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.


Update:

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.

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

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.

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.

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.



来源:https://stackoverflow.com/questions/53848083/maven-aspectj-plugin-non-spring-project-wont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!