Not able to integrate AspectJ with Maven - maven

I banged my head for two days to integrate aspectj with maven, But did not work.
I am writing a simple program with two advices (which works fine in eclipse, so I am sure it's not a code side error).
I have recently started using maven for project building. Can't think of why I am not able to kick off aspectj compiler.
During compilation through maven - I get only class file for java without weaving. .aj file is not compiled.
Please Help!!!!
the first aspect file - ExceptionAspects.aj
package com.aspectSample;
public aspect ExceptionAspects {
pointcut ExceptionHandler(Exception e) : handler(Exception+) && args(e) && within(com.clickable.*);
pointcut callbeforeMethod():execution( public void HelloWorldExclude.*(..)) ;
before(Exception ex) : ExceptionHandler(ex)
{
System.out.println("Added Aspects before Exception :"+ex.toString());
}
before(): callbeforeMethod()
{
System.out.println("Aspect Before Method Call");
}
pointcut sysOutOrErrAccess() : get(* System.out) || get(* System.err);
declare error
: sysOutOrErrAccess()
: "Don't write to the console";
}
The source java file HelloWorldExclude.java
package com.aspectSample;
import java.sql.SQLException;
public class HelloWorldExclude {
private void FoulIntro(String message, String name) throws SQLException
{
throw new SQLException("WriteSomething");
}
public void GiveMeFoulIntro(String message, String name)
{
try
{
this.FoulIntro(message, name);
}catch(SQLException exp)
{
System.out.println("catching exception");
}
}
}
and the pom.xml file is
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.clickable.HelloWorld-Maven</groupId>
<artifactId>HelloWorldAspect-Maven</artifactId>
<version>1.0.0</version>
<name>HelloWorld Aspect</name>
<packaging>jar</packaging>
<description>Hello World Code</description>
<properties>
<java-version>1.6</java-version>
<org.aspectj-version>1.6.11</org.aspectj-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
</dependencies>
</project>

A couple of things that you can try:
Explicitly provide the dependencies of the maven-aspectj plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
with the version of aspectjrt and aspectjtools matching the version of aspectj that you are using.
Do you by any chance have any of your sources in the test folder,
with the src/test folder, then to weave the aspects to the test sources you will have to explicitly mention that in the maven-aspectj plugin:
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>

There was a stupid mistake in pom file.
aspectj plugin was added under element, which is just a configuration of all plugins. It doesn't tell compiler what to use.
It worked as soon as I removed plugin out of this element.
Thanks
Pankaj

Related

Maven Surefire does not give correct test counts when executing Spock tests in parallel

My project contains a series of Groovy integration test scripts for a few APIs. Previously, these tests executed concurrently and took up to 3 hours to execute. I was able to reduce this time to just 5 minutes by using Spock's parallel execution feature.
The new problem is that the Maven Surefire reports no longer give the correct test counts for each test suite. The total test count is correct (more or less), but Surefire is mixing up which tests go in which report.
Here is my pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dsg.payments</groupId>
<artifactId>e2e</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>This will be used to execute normal credit card End2End test cases</description>
<properties>
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<build-helper-maven-plugin.version>3.1.0</build-helper-maven-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-surefire-plugin.version>3.0.0-M3</maven-surefire-plugin.version>
<gmavenplus-plugin.version>1.11.0</gmavenplus-plugin.version>
<swagger-annotations-version>1.6.0</swagger-annotations-version>
<groovy.version>3.0.7</groovy.version>
<spock.version>2.0-M4-groovy-3.0</spock.version>
</properties>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>${spock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotations-version}</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>0.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.15</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<testSourceDirectory>${project.basedir}/src/test/groovy</testSourceDirectory>
<resources>
<resource>
<directory>src/test/groovy</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/groovy</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<plugins>
<!-- Groovy compiler -->
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>${gmavenplus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>addTestSources</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Java compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- Unit Tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<includes>
<include>**/*.java</include>
</includes>
<skipTests>false</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
Here is an example of a test class
package com.dsg.payments.e2e.creditcard
import com.dsg.payments.paygate.ApiClient
import com.dsg.payments.paygate.api.AuthorizationApi
import com.dsg.payments.paygate.model.PaygateAuthorizationRequest
import com.dsg.payments.paygate.model.PaygateAuthorizationResponse
import com.dsg.payments.paygate.model.PaygateEncryptedCardTenderResult
import org.junit.Test
import org.springframework.http.HttpStatus
import org.springframework.web.client.HttpClientErrorException
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Title
import spock.lang.Unroll
import util.Constants
import util.CreditCardType
import util.EntityFactory
#Title("CreditCard-Authorization")
class Authorization extends Specification {
// Constants
#Shared
private static final applicationName = Constants.APPLICATION_NAME
#Shared
private static final referer = Constants.REFERER
#Shared
private static final basePath = Constants.BASE_PATH
#Shared
private AuthorizationApi authorizationApi
def setupSpec() {
// Setup ApiClient to make requests to stage AN01
ApiClient authApiClient = new ApiClient()
authApiClient.setBasePath(basePath)
authorizationApi = new AuthorizationApi(authApiClient)
}
#Test
#Unroll
def "Auth request with valid #cardType card returns authorized response"(CreditCardType cardType) {
when:
PaygateAuthorizationResponse authorizationResponse = authorizationApi.authorize(applicationName, EntityFactory.getPaygateAuthorizationRequest(cardType), referer)
PaygateEncryptedCardTenderResult tenderResult = authorizationResponse.getTenderResults().getEncryptedCardTenderResults().get(0)
then:
// No exception thrown by authorization api client means successful 201 response
authorizationResponse != null
where:
cardType << [
CreditCardType.VISA_Credit_1,
CreditCardType.VISA_Debit,
CreditCardType.VISA_Commercial_Debit,
CreditCardType.VISA_Commercial_Credit,
CreditCardType.VISA_Purchasing_Credit,
CreditCardType.VISA_3DS_Not_Enrolled,
CreditCardType.DISCOVER,
CreditCardType.AMERICANEXPRESS_1,
CreditCardType.MASTERCARD_Debit,
CreditCardType.MASTERCARD_Credit,
CreditCardType.MASTERCARD_Premium_Credit,
CreditCardType.DINERS,
CreditCardType.JAPANCREDITBUREAU
]
}
#Test
#Unroll
def "Auth request with valid cvv for #cardType returns cvv response M"(CreditCardType cardType) {
when:
PaygateAuthorizationResponse authorizationResponse = authorizationApi.authorize(applicationName, EntityFactory.getPaygateAuthorizationRequest(cardType), referer)
PaygateEncryptedCardTenderResult tenderResult = authorizationResponse.getTenderResults().getEncryptedCardTenderResults().get(0)
then:
// No exception thrown by authorization api client means successful 201 response
authorizationResponse != null
tenderResult.getCvvResponse() == "M" // Raw CVV result code for "match"
where:
cardType << [
CreditCardType.VISA_Credit_2,
CreditCardType.VISA_Debit,
CreditCardType.VISA_Commercial_Debit,
CreditCardType.VISA_Commercial_Credit,
CreditCardType.VISA_Purchasing_Credit,
CreditCardType.VISA_3DS_Not_Enrolled,
CreditCardType.DISCOVER,
CreditCardType.AMERICANEXPRESS_2,
CreditCardType.MASTERCARD_Debit,
CreditCardType.MASTERCARD_Credit,
CreditCardType.MASTERCARD_Premium_Credit,
CreditCardType.DINERS,
CreditCardType.JAPANCREDITBUREAU
]
}
// ...
}
There are 3 more test classes (Capture, Refund, and Cancel) with a similar structure that use the #Unroll annotation to re-run the same test with different credit card numbers.
Interestingly, when using Spock parallel execution, Surefire adds an extra test to the report for each test method - one for each credit card and an additional test for the method itself. This is not a huge problem in itself, but it is less than ideal. The real problem is that the Surefire report mixes the classes together (for example, Capture tests end up in the Refund, Cancel, or Authorization reports). This makes it hard to tell from the report which tests failed.
I'll also include my SpockConfig.groovy, which configures the parallel execution:
runner {
filterStackTrace false
optimizeRunOrder true
parallel {
enabled true
dynamic(4.0)
}
}
It has taken much trial and error to get the pom in a state where the tests are all executing correctly in parallel fashion. This is the only combination of dependencies, plugins, and versions that works so far. The only thing that is wrong are the Surefire reports. Any ideas what I am missing?
First: I notice the surefire version in the POM shown is 3.0.0-M3. The mvnrepository.com maven-surefire-plugin list shows the latest is 3.0.0-M5 and a lot of the changes in the newer versions appear to be report related.
Second: check the Maven output log to see if the maven-surefire-report-plugin is being used. If it's there, ensure it is using the same version of Surefire as was used to run the tests. You can do that by adding this to the POM:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
You may not be using Failsafe for integration tests yet, but it's a good idea to keep the Surefire family of plugins using the same version. So when I need to adjust Surefire version, I set them all in the hope of saving someone future troubleshooting pain.

Using AspectJ to recognize method calls

I like to use AspectJ to recognize when a certain method is called within another OSGi bundle I don't have the source code for. Therefore, I build an aspect-bundle. For learning puposes the other bundle is implemented by a sample bundle which simply implements a "sayHello" method. The aspect-bundle is now supposed to recognize whenever this method is called.
Here is what I have so far:
normal bundle
HelloWorld.java
package com.mango.sample.helloworld;
public class HelloWorld {
public void sayHello() {
System.out.println("Hello world");
}
}
Activator.java
package com.mango.sample.helloworld;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
private static BundleContext context;
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
HelloWorld test = new HelloWorld();
test.sayHello();
}
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mango.sample</groupId>
<artifactId>helloworld</artifactId>
<version>0.1</version>
<packaging>bundle</packaging>
<name>helloworld</name>
<prerequisites>
<maven>3.0.4</maven>
</prerequisites>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Import-Package>*</Import-Package>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.name}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.mango.sample.helloworld.Activator</Bundle-Activator>
</instructions>
<manifestLocation>${project.basedir}/META-INF</manifestLocation>
</configuration>
</plugin>
</plugins>
</build>
</project>
aspect-bundle
Activator.java
package com.mango.sample.helloworldj;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
private static BundleContext context;
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
}
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}
}
AspectJTest.aj
package com.mango.sample.helloworldj;
public aspect AspectJTest {
before() : execution(* *.sayHello()) {
System.out.println("Before execution sum");
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mango.sample</groupId>
<artifactId>helloworldj</artifactId>
<version>0.1</version>
<packaging>bundle</packaging>
<name>helloworldj</name>
<prerequisites>
<maven>3.0.4</maven>
</prerequisites>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Import-Package>*</Import-Package>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.name}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.mango.sample.helloworldj.Activator</Bundle-Activator>
<Export-Package>com.mango.sample.helloworldj;aspects=AspectJTest</Export-Package>
</instructions>
<manifestLocation>${project.basedir}/META-INF</manifestLocation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.3</version>
<configuration>
<complianceLevel>1.5</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
After compiling, I start equinox (separately from eclipse)
configuration/config.ini
osgi.framework.extensions=org.eclipse.equinox.weaving.hook_1.0.200.I20130319-1000.jar
osgi.bundles=\
reference\:file\:../lib/org.apache.felix.fileinstall-3.1.6.jar#1:start,\
reference\:file\:../lib/org.eclipse.osgi.services-3.3.100.v20120522-1822#2:start,\
reference\:file\:../lib/org.eclipse.equinox.console-1.0.0.v20120522-1841.jar#start,\
reference\:file\:../lib/slf4j-api-1.7.2.jar#1:start,\
reference\:file\:../lib/logback-classic-1.0.9.jar#1:start,\
reference\:file\:../lib/logback-core-1.0.9.jar#1:start,\
reference\:file\:../lib/org.aspectj.runtime_1.7.3.20130613144500-a.jar#2:start
and the options:
-Daj.weaving.verbose=true
-Dorg.aspectj.weaver.showWeaveInfo=true
-Dorg.aspectj.osgi.verbose=true
Then, I install and start the aspect bundle and the other bundle afterwards. After starting the HelloWorld bundle, I expected something like this
Before execution
Hello world
But I got only Hello world. I guess there is going something wrong with weaving the aspects into the helloworld bundle. Is there something missing in the pom.xml files?
How can I solve it?

AspectJ + Junit + Maven + Java8

I followed this SO question and tried to implement it for java8. My project is not a spring project.
Aspect
#Aspect
public class MethodLogger {
#Pointcut("execution(#org.junit.Test * *())")
public void testMethodEntryPoint() {}
#Before("testMethodEntryPoint()")
public void executeBeforeEnteringTestMethod() {
System.out.println("EXECUTE ACTION BEFORE ENTERING TEST METHOD");
}
#After("testMethodEntryPoint()")
public void executeAfterEnteringTestMethod() {
System.out.println("EXECUTE ACTION AFTER ENTERING TEST METHOD");
}
}
JUnit Test
#RunWith(JUnit4.class)
public class POSTaggerTest {
#Test
public void test() {
...
}
}
POM.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</aspectLibrary>
</aspectLibraries>
<!-- java version -->
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
<!-- End : java version -->
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
</configuration>
</execution>
</executions>
</plugin>
:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<type>maven-plugin</type>
</dependency>
I don't see any error. Am I missing something? Or any wrong artifact? What I want when I run junit tests, all the aspects whould work fine.
The situation you want to recreate is like this:
There is one Maven module with aspects. It is compiled with AspectJ Maven Plugin.
There is another module with the actual application and test code. It is also compiled with AspectJ Maven, this time referring to the first module as an aspect library.
What you are doing though is:
You have a single module. This is not a problem in and of itself because it is easily possible to keep aspects and Java code within the same module if you want the aspects applied on this module only.
But now you declare JUnit as an aspect library. Why? It does not contain any aspects. You should remove that declaration.

maven plugin descriptor not getting generated

I'm creating a maven plugin, MVN clean install build succeeds but plugin.xml is not getting generated.
#Mojo( name = "cover", defaultPhase = LifecyclePhase.POST_INTEGRATION_TEST)
public class RunCoverage extends AbstractMojo
{
#Parameter( property = "cover.wadl", defaultValue = "test")
private String wadl;
#Parameter( property = "cover.endpoints",defaultValue = "test")
private String endpoints;
#Override
public void execute() throws MojoExecutionException
{
<somecode>
}
}
And the pom.xml is
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>end-point-test-coverage</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<executions>
<execution>
<id>default-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
<phase>process-classes</phase>
</execution>
<execution>
<id>help-descriptor</id>
<goals>
<goal>helpmojo</goal>
</goals>
<phase>process-classes</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Maven clean install doesn't generate plugin.xml
When used in a dependent project, I'm getting the following error
Failed to parse plugin descriptor for it.gruppopam.common:end-point-test-coverage:1 (/home/d/.m2/repository/it/common/end-point-test-coverage/1/end-point-test-coverage-1.jar): No plugin descriptor found at META-INF/maven/plugin.xml -> [Help 1]
[ERROR]
First i would try to set the packaging type to maven-plugin instead of the default which is jar. Furthermore i would suggest to use more up-to-date versions of plugins (maven-compiler-plugin: 3.1) and use a more up-to-date version of maven-plugin-api (3.0? but not 2.0).
you have to remember to change it in your pom.xml to:
<packaging>maven-plugin</packaging>
it is by default:
<packaging>jar</packaging>

Mock aspect in junit failed to apply

I'm building up a simple project to learn aspectj.
It's from aspect in action 2nd and the idea is very simple ---- the MessageCommunicator will be responsible for delivering the message and it's the main business logic. Meanwhile, Authenticator will be responsible for authentication and will be weaved as declared SecurityAspect.
Though it's very straightforward to see in the log that the aspect is working. Still I want to ensure it works in junit case.
In my project, I'm using maven 3.0.4 with aspectj 1.7.3 and aspect-maven-plugin 1.5.
Now the problem is below warning is there when compile the test case. As the consequence, the aspects in test package doesn't work. However, if you write a Main class in source package and run, the aspect in source package will work.
The warning message while build:
[INFO] --- aspectj-maven-plugin:1.5:test-compile (test-compile_with_aspectj) # aspectj ---
[WARNING] advice defined in org.javen.study.aspectj.c02.aspects.MockAuthenticationAspect has not been applied [Xlint:adviceDidNotMatch]
[WARNING] advice defined in org.javen.study.aspectj.c02.aspects.SecurityAspect has not been applied [Xlint:adviceDidNotMatch]
[WARNING] advice defined in org.javen.study.aspectj.c02.aspects.TrackingAspect has not been applied [Xlint:adviceDidNotMatch]
I will also attach all the related source code below:
MessageCommunicator who is responsible for the main business:
package org.javen.study.aspectj.c02;
public class MessageCommunicator {
public void deliver(String message) {
System.out.println(message);
}
public void deliver(String person, String message) {
System.out.println(person + ", " + message);
}
}
Simple version of authenticator which will do the authentication:
public class Authenticator {
public void authenticate() {
System.out.println("authenticated");
}
}
SecurityAspect which will advice on MessageCommunicator:
package org.javen.study.aspectj.c02.aspects;
import org.javen.study.aspectj.c02.Authenticator;
public aspect SecurityAspect {
private Authenticator authenticator = new Authenticator();
declare warning
: call(void Authenticator.authenticate())
&& !within(SecurityAspect)
: "Authentication should be performed only by SecurityAspect";
pointcut secureAccess() : execution(* org.javen.study.aspectj.c02.MessageCommunicator.deliver(..));
before() : secureAccess() {
System.out.println("Checking and authenticating user");
authenticator.authenticate();
}
}
MockAuthenticationAspect in test package to advice the authenticator to inject some verification logic(no need to look into advice detail, the advice implementation is problematic):
package org.javen.study.aspectj.c02.aspects;
import org.javen.study.aspectj.c02.Authenticator;
public aspect MockAuthenticationAspect {
declare parents: Authenticator implements Callable;
private boolean Callable.isCalled;
public void Callable.call() {
isCalled = true;
}
public boolean Callable.isCalled() {
return isCalled;
}
Object around(Callable accessTracked) : execution(* Authenticator.authenticate(..))
&& !execution(* Callable.*(..))
&& this(accessTracked) {
accessTracked.call();
return null;
}
private static interface Callable {
}
}
The pom of whole project:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.javen.study</groupId>
<artifactId>aspectj</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<aspectj.version>1.7.3</aspectj.version>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</version>
<configuration>
<complianceLevel>1.6</complianceLevel>
<includes>
<include>**/*.java</include>
<include>**/*.aj</include>
</includes>
</configuration>
<executions>
<execution>
<id>compile_with_aspectj</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile_with_aspectj</id>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
When build with command "mvn clean install", the warning texts will be printed and the test package advice will not work. However, if check with AJDT in eclipse, all the pointcut and advice is working.
Could someone help me? Thanks a lot.
The problem is solved by added below configuration in test-compile execution.
<execution>
<id>test-compile_with_aspectj</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<weaveDirectories>
<weaveDirectory>target/classes</weaveDirectory>
</weaveDirectories>
</configuration>
</execution>
Don't know it's a good pratice or not, but at least now it works.

Resources