Using AspectJ annotations in maven project: weaving is not working - java

I am trying to use AspectJ in a simple project without using Spring, and while I have seen similar questions and my code seems to be correct, I don't understand why it's not working. I'm using Eclipse Oxygen 4.7.3 (not using AJDT tools), JDK 7, maven 3.5.2, and my code is as follows:
pom.xml
<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</groupId>
<artifactId>aspect-tutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.plugin.version>3.5.1</maven.compiler.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<configuration>
<complianceLevel>1.7</complianceLevel>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
MainApp.java
package com.pkg;
public class MainApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
HelloWorld a = new HelloWorld();
a.printHello();
}
}
HelloWorld.java
package com.pkg;
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void printHello() {
System.out.println("Print Hello...");
}
}
TestAspect.java
package com.pkg;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
#Aspect
public class TestAspect {
#Before("execution(* com.pkg.HelloWorld.printHello(..))")
public void testBefore2(){
System.out.println("Yeeha");
}
}
Running mvn clean install is successful, but the output only prints the "Print Hello..." part. Should I use a different approach? (Maybe use a .aj file instead, or try load-time-weaving) Any help appreciated.

The problem is the configuration of both AspectJ Maven and Maven Compiler. My POMs for AspectJ usually look a bit different than yours (a few more settings), but here is yours with minimal changes in order to make it work:
<?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</groupId>
<artifactId>aspect-tutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.plugin.version>3.5.1</maven.compiler.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<complianceLevel>1.7</complianceLevel>
<source>1.7</source>
<target>1.7</target>
</configuration>
<executions>
<execution>
<!-- IMPORTANT -->
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<!-- IMPORTANT -->
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainClass>com.pkg.MainApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
See how I set incremental compilation to false for Maven compiler? This is due to an old bug (still unfixed) which actually inverts the switch, so in order to make incremental compilation work you have to "deactivate" it. Very weird.
You also need to define executions for the process-sources phase for AspectJ Maven.
Besides, I upgraded to AspectJ Maven 1.11 and thus also to AspectJ runtime 1.8.13.
I also added Maven Exec plugin in order to easily prove that it is working now. Just call mvn clean compile exec:java and check the output:
(...)
[INFO] --- aspectj-maven-plugin:1.11:compile (default) # aspect-tutorial ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) # aspect-tutorial ---
[INFO] Nothing to compile - all classes are up to date
(...)
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) # aspect-tutorial ---
Yeeha
Print Hello...
(...)
Otherwise I support what NĂ¡ndor said: Make sure you use AspectJ plugins for Eclipse or IDEA if you also want to run your aspect-enhanced Java code from an IDE.

How you you run your app? Purely from maven or from within Eclipse? Do you have Eclipse to automatically build your projects? If yes, you probably won't have too much success with compile time weaving, because Eclipse will overwrite your Maven built classes with Eclipse built classes. Without the AJDT feature installed, and properly set up workspace project with AspectJ nature, the resulting compiled code won't be "enhanced" by the AspectJ weaver.

Related

How can I configure the moditect-maven-plugin to use the auto-value library in my modular application?

I'm trying to build a modular application, and I'm attempting to use the moditect-maven-plugin in order to use the auto-value library (which is not modular).
Based on the instructions here, I came up with the following 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>org.example</groupId>
<artifactId>moditect-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value-annotations</artifactId>
<version>${auto-value.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>${auto-value.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.moditect</groupId>
<artifactId>moditect-maven-plugin</artifactId>
<version>1.0.0.Beta2</version>
<executions>
<execution>
<id>generate-module-info</id>
<phase>generate-sources</phase>
<goals>
<goal>generate-module-info</goal>
</goals>
<configuration>
<modules>
<module>
<artifact>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value-annotations</artifactId>
<version>${auto-value.version}</version>
</artifact>
<moduleInfo>
<name>com.google.auto.value.annotations</name>
<exports>
com.google.auto.value;
*;
</exports>
</moduleInfo>
</module>
</modules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<auto-value.version>1.7</auto-value.version>
</properties>
The important portion is the moditect-maven-plugin configuration. Since I need the com.google.auto.value.AutoValue annotation, I export that package and then give a relevant name to the module.
Just for reference, here's the class I'm using the annotation in:
package com.example.moditect.test;
import com.google.auto.value.AutoValue;
#AutoValue
abstract class Animal {
abstract String name();
abstract int numberOfLegs();
static Builder builder() {
return new AutoValue_Animal.Builder();
}
#AutoValue.Builder
abstract static class Builder {
abstract Builder setName(String value);
abstract Builder setNumberOfLegs(int value);
abstract Animal build();
}
}
...and here's the module-info.java file of my application:
module com.example.moditect.test {
requires com.google.auto.value.annotations;
}
However, when I run mvn clean install, I get the module not found error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project moditect-test: Compilation failure
[ERROR] /home/someuser/development/workspace/moditect-test/src/main/java/module-info.java:[2,35] module not found: com.google.auto.value.annotations
It does appear that the moditect-maven-plugin is working based on these logs that get outputted when I run mvn clean install:
[INFO] --- moditect-maven-plugin:1.0.0.Beta2:generate-module-info (generate-module-info) # moditect-test ---
writing to /home/someuser/development/workspace/moditect-test/target/moditect/auto.value.annotations/module-info.java
[INFO] Created module descriptor at /home/someuser/development/workspace/moditect-test/target/generated-sources/modules/com.google.auto.value.annotations/module-info.java
And for what it's worth, here's the generated module-info of this auto-value library (from the location that's logged above):
module com.google.auto.value.annotations {
exports com.google.auto.value;
exports com.google.auto.value.extension.memoized;
}
So I need some help figuring out what I did wrong. I'm assuming I just messed up the configuration of the moditect-maven-plugin, but I'm not sure how.
I made the silly mistake of using the goal generate-module-info instead of add-module-info. Just switching the goal with the correct one solved this issue.

Maven Custom Plugin Creation and Execution

I am trying to create a custom parameter less plugin by referring to create simple mojo creation instructions by apache in the below link.
https://maven.apache.org/guides/plugin/guide-java-plugin-development.html#
But I am unable to run the same using maven goal. Kindly suggest what I am missing. As I am new to maven and there aren't many tutorials on custom mavem plugin creation. Any suggestions on this topic will be helpful to me. Thanks in advance.
pom.xml
<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>sample.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>Sample Parameter-less Maven Plugin</name>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.0</version>
</dependency>
<!-- dependencies to annotations -->
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>sample.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>sayhi</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
GreetingMojo.java
package sample.plugin.hello_maven_plugin;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
/**
* Says "Hi" to the user.
*
*/
#Mojo( name = "sayhi")
public class GreetingMojo extends AbstractMojo
{
public void execute() throws MojoExecutionException
{
getLog().info( "Hello, world." );
System.out.println("My first maven plugin.");
}
}
I am trying to run the pom.xml using package command and mvn install command.
Generally you shouldn't execute the plugin goal in the plugin you are trying to build. The reason you cannot build the plugin with mvn install is because you have configured maven to expect the plugin to already be built.
This section belongs in a different POM file, not the plugin POM file.
<build>
<plugins>
<plugin>
<groupId>sample.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>sayhi</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
After you have removed this section from the plugin POM you can run mvn install to install your plugin locally. Once installed you can re-add the section to the POM if you really wanted to, but ideally you would add it to a completely different maven project and execute maven in that project.

How to run junit tests on a non java project

I have a project folder but it is not a java project. It is a maven project. I have written a junit test and it runs perfectly when running in the eclipse IDE but when I run the maven command mvn install, it seems to skip my junit tests. I have included the test file in src/test/java/ (the name of my test is AppTest.java) and the main .java file (with the main method) is in src/main/java/. I have noticed that the project I am currently working on is a maven project and not a maven java project. I have included a screen of my current folder structure:
folder structure
Maven test output <- should not build as I have a deliberate test that should fail
This is the POM. I have deleted/commented out some sensitive parts so the pom file may be syntactically wrong but the main plugins I use are there; tap4j, junit and surefire.
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>integration-api-parent</artifactId>
<groupId>uk.gov.dwp</groupId>
<version>1.0.2</version>
</parent>
<artifactId>aa</artifactId>
<version>1.0.6</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>aa</finalName>
<plugins>
<!-- plugin>
<groupId>com.github.fracpete</groupId>
<artifactId>latex-maven-plugin</artifactId>
<configuration>
<forceBuild>true</forceBuild>
</configuration>
</plugin>
<plugin>
<groupId>com.github.fracpete</groupId>
<artifactId>latex-maven-plugin</artifactId>
<configuration>
<forceBuild>true</forceBuild>
</configuration>
</plugin-->
<plugin>
<!-- Plug-in utilised for the execution of the JMeter Integration Tests -->
<!-- These tests are executed against the nominated integration server where as -->
<!-- instance of AA exists -->
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>1.9.0</version>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreResultErrors>false</ignoreResultErrors>
<suppressJMeterOutput>false</suppressJMeterOutput>
<overrideRootLogLevel>INFO</overrideRootLogLevel>
</configuration>
</plugin>
<plugin>
<!-- Step to copy the latest plug-ins that form this build to the integration server -->
<!-- This is done using the SCP command via the ANT plug-in thus allowing it to execute on all platforms -->
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-jsch</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.42</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.tap4j/tap4j -->
<dependency>
<groupId>org.tap4j</groupId>
<artifactId>tap4j</artifactId>
<version>4.2.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
<!-- plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/cassandra-assembly.xml</descriptor>
<descriptor>src/main/assembly/devenv-assembly.xml</descriptor>
<descriptor>src/main/assembly/main-assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
<!-- plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<configuration>
<skipPoms>false</skipPoms>
<sourceDirectory>${project.basedir}/src/main/latex-templates</sourceDirectory>
<outputDirectory>${project.build.directory}/latex</outputDirectory>
</configuration>
</plugin-->
</plugins>
</build>
</project>
AppTest:
package AccessGateway;
import static org.junit.Assert.*;
import java.io.File;
import org.junit.Test;
import org.tap4j.consumer.TapConsumer;
import org.tap4j.consumer.TapConsumerFactory;
import org.tap4j.model.TestSet;
public class AppTest {
Practise prac;
final String DIRECTORY = "C:\\Users\\Hello\\Desktop\\";
#Test
public void testHeaderProcessor() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"TAPHeaderProcessor.txt")));
}
#Test
public void testHeaderPortForward() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"TAPHeaderPortForward.txt")));
}
#Test
public void catunittest() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"catunittest.txt")));
}
#Test
public void catunitcrowstest() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"catcrowd.txt")));
}
#Test
public void testCrowd() {
prac = new Practise();
assertFalse(
prac.runTest(new File(DIRECTORY+"TAPCrowd.txt")));
}
#Test
public void testADFS() {
prac = new Practise();
assertFalse(
prac.runTest(new File(DIRECTORY+"TAPADFSformat.txt")));
}
}
The problem is the packaging of your project which is pom
You can't execute Surefire on this kind of project.
Try adding surefire plugin. When i have tests in my app i always include it (works for junit as well as testng). Based on your logs i can see that you dont have it declared.
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>

Error while compiling when using AspectJ compiler instead of Javac

I have a multi module project. The aspect is currently added to the "core" project. When doing a mvn clean install here it works. However trying to do a mvn clean install on parent project it fails with this error when compiling one of the other projects:
The type org.hibernate.annotations.CacheConcurrencyStrategy cannot be resolved. It is indirectly referenced from required .class files
If I add the Hibernate core dependency in that project too it works, but adding dependencies to projects that shouldn't have the dependency does not make sense - so it is not a solution. When compiling with javac it works fine.
What is the reason? And how can I fix it so I can use the AspectJ compiler without leaking dependencies to projects that shouldn't have that?
I have this configuration in the parent POM:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<complianceLevel>1.6</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Update
I just found out. Running mvn clean install fails every time. However, running mvn [clean] install one time fails. Then running mvn install without clean works. I see that the builddef.lst in the target folder is the reason why it works and fails based on whether or not you run clean. So now my question is: How do you automatically generate this file?
Parent POM-file:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>core-lib</artifactId>
<name>core-lib</name>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<complianceLevel>1.6</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
</dependency>
</dependencies>
<modules>
<module>core-xyz</module>
<module>core-xyz2</module>
</modules>
</project>
Enable debug on the maven invocation to dig deeper. You should observe that the aspectj compile is only being invoked during the first maven invocation with the clean. Since the builddef.lst already exists after the first invocation, invoking without clean skips the aspectj compile.
This aspectj compile plugin behavior has been observed previously and was described here:
http://out-println.blogspot.com/2007/08/compile-time-checks-with-aspectj-part-2.html?m=1
You will need to look deeper to address the underlying issue, but as one commenter has already suggested, the aspectj compiler should only be enabled in modules that require it.
Otherwise, the additional dependencies are required for the aspectj compile, as you have already observed. I have incorporated aspectj compile into my own work without issue by restricting it to only the modules that require it.
According to the AspectJ compiler Maven plugin you could set up the argumentFileName to locate an existing builddef.lst.
So you can generate the builddef.lst and copy it to your resources folder, and instruct the AspectJ Maven plugin to use that file.

Generating UUID through Maven

I need to set a property in maven pom.xml file which should be a UUID. Can anybody tell me
what is the best possible way to set a property to UUID?
I am using a profile which launch the gigaspaces and gigaspaces requires group name which I
want to be unique(uuid). So, in my profile I want to set a groupName property value which
should change for each build. I wrote a UUIDGenerator plugin myself as I didn't found any.
So, I am looking How can this be achieved? Is writing a plugin better option or there is an
easier option.
Thanks,
Shekhar
Arian's solution (implementing a maven plugin) is IMO a clean way to implement your requirement (and +1 for his answer).
But if you don't plan to reuse your plugin somewhere else, a quick alternative would be to hack the pom using the GMavenPlus plugin. Here is an example showing how to do so using a Java class from a library to generate some uuid and set it as a property:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>Q3984794</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.safehaus.jug</groupId>
<artifactId>jug</artifactId>
<version>2.0.0</version>
<!-- the classifer is important!! -->
<classifier>lgpl</classifier>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>set-custom-property</id>
<phase>initialize</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<classpath>
<element>
<groupId>org.safehaus.jug</groupId>
<artifactId>jug</artifactId>
<classifier>lgpl</classifier>
</element>
</classpath>
<source>
import org.safehaus.uuid.UUIDGenerator
def uuid = UUIDGenerator.getInstance().generateRandomBasedUUID()
project.properties.setProperty('groupName', uuid.toString())
</source>
</configuration>
</execution>
<execution>
<id>show-custom-property</id>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
def props = project.properties
props.each {key, value -> println key + "=" + value}
</source>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Just bind the plugin to a phase prior to the gigaspaces stuff.
The second execution is just there for demonstration purpose (to show the properties):
$ mvn generate-resources
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Q3984794 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- gmaven-plugin:1.3:execute (set-custom-property) # Q3984794 ---
[INFO]
[INFO] --- gmaven-plugin:1.3:execute (show-custom-property) # Q3984794 ---
downloadSources=true
downloadJavadocs=true
project.reporting.outputEncoding=UTF-8
project.build.sourceEncoding=UTF-8
groupName=814ff1a5-a102-426e-875c-3c40bd85b737
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...
First of all, if your set up requires something called "group name", you probably should provide a meaningful value. If it has to be unique, you can append some generated characters, like "MyApplication-10937410". Also, using a UUID seems to me like using a sledge-hammer to crack a nut. But this is independent of your actual problem, so here is the solution I propose:
If you have not already done so, create a maven plugin (there's an archetype for that). Add this dependency:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.2.1</version>
</dependency>
This is how your MOJO should look like:
/**
* Goal which generates a group name.
*
* #goal generate
* #phase initialize
*/
public class GroupNameGeneratorMojo extends AbstractMojo {
/**
* #parameter expression="${project}"
* #required
* #readonly
*/
private MavenProject project;
#Override
public void execute() throws MojoExecutionException {
String groupName = ... ;
project.getProperties().setProperty("uniqueGroupName", groupName);
}
}
In your actual projects pom, use ${uniqueGroupName} whereever you need it and configure your plugin like this
<build>
<plugins>
<plugin>
<groupId>the.plugin.groupid</groupId>
<artifactId>groupNameGenerator</artifactId>
<executions>
<execution>
<goals><goal>generate</goal></goals>
</execution>
</executions>
<plugin>
There is https://github.com/stevespringett/maven-uuid-generator which exposes a uuid for the build as ${project.build.uuid}. You can use it like
<plugins>
<plugin>
<groupId>us.springett</groupId>
<artifactId>maven-uuid-generator</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

Categories