export AspectJ project as jar and use in another application - annotations

I am writing an Aspect in Mavenised project. PROJECT_A
Now I want to use the PROJECT_A.jar in PROJECT_B which is non mavenised.
And in PROJECT_B i want to use the annotaition which I have written using the aspect in PROJECT_A .
I tried exporting PROJECT_A.jar - with aspectJ Plugin as follows. But when i use it in PROJECT_B - my annotation does not works as expected.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
I am new to the use of AspectJ and I am not getting how to do it without maven in PROJECT_B.
Please help , any help is appreciated :)

First of all, upgrade to AspectJ Maven 1.9.
Then mavenise project B! This enables you to you can use AspectJ Maven plugin to weave the aspects into your other module, even after compilation. This is called binary weaving. Please read the plugin documentation, especially the info about parameters aspectLibraries and weaveDependencies, depending on which approach you want to pursue.
Alternatively, start your JVM with -javaagent:/path/to/aspwctjweaver.jar and use the AspectJ weaving agent for load-time weaving (LTW). But do not forget then to also provide a LTW configuration via META-INF/aop.xml.
Either way, you have options. But both require you to read some documentation or at least search this site for examples.

Related

Eclipse overwrites manifest created by maven bundle plugin

I'm using maven-bundle-plugin to generate MANIFEST.MF for OSGi container.
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>com.example.mypackage</Export-Package>
</instructions>
</configuration>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
However, that manifest file got destroyed (overwritten) by Eclipse (4.6.1 Neon) after, f.g Maven/Update Project or Clean/Build.
Is it possible to make Eclipse somehow aware of the maven plugins, and not destroy their output? What should I setup to prevent that (mis)behaviour?
If that issue is not to be fixed with Eclipse, does it work better with IntelliJ, for example? OSGi support within IDE is quite important for me...
I have to change my answer. I missed the part that you define the goal manifest. This is the new and recommended way to use the maven bundle plguin but it requires that you tell the jar plugin to use the existing Manifest.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
old answer
I suspect you are using the Eclipse PDE. The PDE is always working Manifest first. This means it is incompatible with the maven-bundle-plugin.
I recommend to use a plain maven build which we use a lot at apache projects like Apache Karaf or Apache Aries. It lacks the special OSGi support of Eclipse PDE but this sucks anyway.
You can augment this by using the eclipse plugin bndtools. Bndtools now provides maven support to a degree. This means you can now provide maven based OBR indexes for your project and define which bundles to start in a bndrun file. This allows to directly start and debug your OSGi project in Eclipse. See the osgi-chat example on how to do it.
Beware though that bndtools just started with maven support recently. So the current version 3.3.0 still lacks some of the convenience for maven builds.
With my configuration:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.3.0</version>
<extensions>true</extensions>
<configuration>
<manifestLocation>src/main/resources/META-INF</manifestLocation>
<rebuildBundle>true</rebuildBundle>
<instructions>
<Bundle-SymbolicName>${project.artifactId};singleton:=true</Bundle-SymbolicName>
</instructions>
</configuration>
</plugin>
And the Plugins installed:
m2e Connector: http://download.eclipse.org/m2e-wtp/releases/neon/
BND Tools: http://bndtools.org/
The Eclipse build finally didn't destroy the manifest file. But there were some iterations(maven build, Update Maven Dependencies, Eclipse Build, maven build) necessary. Also beforehand, I deleted all project related settings on the filesystem: .classpath, .project, .factorypath, .settings, .apt_generated, target. Hope that helps.

Karaf - generating feature file and creating custom distribution

I have an application which I want to deploy in karaf. I have created a feature file and I am able to add features through this file using karaf console. What I want to achieve now is that create this feature file through maven commands instead of creating it manually and then create a custom karaf distribution using this feature file. How can I achieve it ?
My approach so far is to create a maven module for generating feature file using karaf-maven-plugin and then create another module to generate karaf custom distribution so that we dont need to access maven in production environment.
Is this approach correct ? Do I really need to make two different modules for achieving it. How can I get access to feature file from second module.
These are my poms -
all dependecies
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.karaf.tooling</groupId>
<artifactId>karaf-maven-plugin</artifactId>
<version>4.0.5</version>
<extensions>true</extensions>
<executions>
<execution>
<id>generate</id>
<phase>generate-resources</phase>
<goals>
<goal>features-generate-descriptor</goal>
</goals>
<configuration>
<startLevel>80</startLevel>
<aggregateFeatures>true</aggregateFeatures>
<includeTransitiveDependency>true</includeTransitiveDependency>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I am not able to figure out the second part yet. Any help with that is really appreciated.
To generate a custom Karaf you just need to use the karaf-maven-plugin.
For example the following will generate a fully working custom Karaf:
<plugin>
<groupId>org.apache.karaf.tooling</groupId>
<artifactId>karaf-maven-plugin</artifactId>
<version>4.0.0</version>
<extensions>true</extensions>
<configuration>
<!-- no startupFeatures -->
<bootFeatures>
<feature>minimal</feature>
</bootFeatures>
<installedFeatures>
<feature>wrapper</feature>
<feature>spring/4.0.7.RELEASE_1</feature>
</installedFeatures>
</configuration>
</plugin>
This will generate a custom karaf based on the minimal sets of features which are needed to create the minimal distro. If you want to depend on the standard distro just exchange that with standard.
Btw. all this is also documented in the Karaf documentation

Require Java 8 to build Scala project using Maven

I'm using scala-maven-plugin to build a Scala project using Maven. My project depends on some libraries from Java 8. I would like to specify in my pom that the project requires Java 8 (using something akin to -target=jdk-1.8, which appears not to exist) so that it will fail a little more elegantly/informatively when someone tries to compile using Java version < 8. Currently it just fails to find the packages I'm trying to import.
I tried adding maven-compiler-plugin with source and target set to 1.8, and this didn't work, presumably because scala-maven-plugin is handling compilation instead of maven-compiler-plugin.
Is this possible?
You can use Maven Enforcer Plugin to enforce some rules to be asserted when running your Maven build.
Here you want to make sure that the Java version used is at least 8. You can achieve this by adding the following to your pom.xml file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>enforce-java</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>[1.8,)</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Spring-ws best way to generate java classes when autogeneration WSDL

I am autogenerating my wsdl using spring-ws and an XSD. Now I would like to generate java classes to return and do marschalling.
However I am seeing a lot of different ways to accomplish this, and not all are as clear about the benefit\detrement.
Some just save the generated WSDL in their project, others use their XSD file to generate, others add XJB etc...
My first thought was just adding:
<plugin>
<groupId>com.sun.tools.xjc.maven2</groupId>
<artifactId>maven-jaxb-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<removeOldOutput>true</removeOldOutput>
<schemaDirectory>src/main/webapp/WEB-INF</schemaDirectory>
</configuration>
</plugin>
to my pom.xml
My preferred way is jaxb2-maven-plugin. See http://mojo.codehaus.org/jaxb2-maven-plugin/usage.html
It's actually using XJC, a command that now comes with your current JDK (under bin in Windows or Commands in Mac)
You can call it directly with xjc -p your.package -wsdl your.wsdl
to use maven to generate java code from WSDL, you can check maven cxf codegen plugin wsdl to java.

What is the correct way to specify a main class when packaging a jar using m2eclipse?

Problem:
I'd like to specify the main class in a jar file that I am packaging using m2eclipse: [right-click] -> Run As -> Maven package. I'm still learning Maven and from what I've read, the straight-up way of accomplishing this task is to add a stanza to the pom.xml.
Here are examples I found when I was researched this issue:
Cannot make executable jar using m2eclipse
http://docs.codehaus.org/pages/viewpage.action?pageId=72602
My question is this: is it okay to manually edit the pom.xml file outside of Eclipse/m2eclipse using a text editor, or should I be doing this configuration using the m2ecplise GUI? There are several tabbed dialog boxes that seem like they might be likely candidates for this task, like "Plugins" and "Build". I looked through Sonatype's documentation and couldn't find any detailed instructions on how to accomplish what I need to do.
I'm a little hesitant to edit the pom.xml manually because I notice the "Effective POM" already has a lot of extra stuff in it, including the plugin definition that needs to have added to it:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
If I understand correctly, the Effective POM needs to be changed so that the plugin is configured like this:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>[name of main class]</mainClass>
<packageName>[package name]</packageName>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${pom.url}</url>
</manifestEntries>
</archive>
</configuration>
</plugin>
Is this right? And if so, do I do this through m2eclipse or should I just copy all the extra Effective POM stuff and paste it into the actual pom.xml using a text editor?
Thanks to anyone who can shed some light.
UPDATE: I went ahead and manually edited the pom.xml file in a text editor. When I viewed Effective POM in m2eclipse it displayed everything that I put in (I assume). I built the jar and the main class was correctly set.
This seems like a hack to me though. Does anyone know if there's a way to do this configuration using m2eclipse itself? I checked the m2eclipse tabs and nothing seemed to have been updated as a result of my manual edits of pom.xml (other than the Effective POM tab).
m2eclipse doesn't do everything for you, i.e. there isn't a pretty UI tab for handling everything.
My team is most comfortable with editing the POM manually, and using the other tabs for verification (like Effective POM view).