Posts Tagged ‘Maven’
How to compile both Java classes and Groovy scripts with Maven?
Case
Your project includes both Java classes and Groovy scripts. You would like to build all of them at the same time with Maven: this must be possible, because, after all, Groovy scripts are run on a Java Virtual Machine.
Solution
In your pom.xml
, configure the maven-compiler-plugin
as follows:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <compilerId>groovy-eclipse-compiler</compilerId> <source>1.6</source> <target>1.6</target> </configuration> <dependencies> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-compiler</artifactId> <version>2.8.0-01</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-batch</artifactId> <version>2.1.5-03</version> </dependency> </dependencies> </plugin>
With such setup, default compiler (which cannot compile Groovy scripts parallelly of Java sources) will be replaced with Eclipse’s one (which can).
(long tweet) Error injecting: org.codehaus.groovy.eclipse.compiler.GroovyEclipseCompiler
Case
On building a Groovy project with Maven, I got the following error:
Error injecting: org.codehaus.groovy.eclipse.compiler.GroovyEclipseCompiler java.lang.NoClassDefFoundError: org/codehaus/plexus/compiler/CompilerResult
Quick fix
Downgrade maven-compiler-plugin
to version 3.0
instead of branch 2.3.X
.
Maven: How to use jars in a lib/ folder?
Case
I have to work on an “old-school” project: without Maven (and even without integration within Eclipse). The JARs depended on by the application are located in a lib/
folder. Besides, I cannot add JARs to company repository.
How to integrate Maven with this lib/
folder?
Solution
First of all, you have to enable the support of Maven within IntelliJ IDEA: select the project > right click > add framework support > select Maven.
Create a pom.xml
and complete the main information.
Identify the JARs which are relatively common (eg those available in http://repo1.maven.org/maven2/
), and add them as dependencies.
Now, you still have to deal with “uncommon” JARs
Quick and (very) dirty
The quickest way is to add the JARs as dependencies, declared with scope system
, eg:
<dependency> <groupId>unknownGroupId</groupId> <artifactId>someArtifactId</artifactId> <version>someVersion</version> <scope>system</scope> <systemPath>${project.basedir}/lib/foo.jar</systemPath> </dependency>
The main drawback of this dirty solution is that when you generate your archive, the JARs depended on with scope system
will not be exported… Quiet a limitation, isn’t it?
(A bit) Slower and (far) cleaner
You have to declare a “remote repository”, of which URL is local, such as:
<repository> <id>pseudoRemoteRepo</id> <releases> <enabled>true</enabled> <checksumPolicy>ignore</checksumPolicy> </releases> <url>file://${project.basedir}/lib</url> </repository>
Then, declare the dependencies like:
<dependency> <groupId>UNKNOWN</groupId> <artifactId>foo</artifactId> <version>UNKNOWN</version> </dependency>
Move and/or rename the JARs, for instance from lib/foo.jar
folder to the actual one, such as lib/UNKNOWN/foo/foo-UNKNOWN.jar
.
Now it should be OK ;-).
In my case I prefered to add Maven Assembly plugin with the goal
, to be sure to build a unique JAR with complete exploded dependencies.
Unable to instantiate default tuplizer… java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.
Case
On running a web application hosted on Jetty, I get the following stracktrace:
Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/classes/config/spring/beans/HibernateSessionFactory.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
Unlike what I immediatly thought at first glance, the problem is not induced by the Tuplizer ; the actual error is hidden at the bottom: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.
Here are some of the dependencies:
org.hsqldb:hsqldb:jar:2.2.8:compile org.springframework:spring:jar:2.5.6:compile org.hibernate:hibernate:jar:3.2.7.ga:compile javax.transaction:jta:jar:1.0.1B:compile | +- asm:asm-attrs:jar:1.5.3:compile | \- asm:asm:jar:1.5.3:compile
Fix
Main fix
The case is a classic problem of inherited depencencies. To fix it, you have to excluse ASM 1.5.3, and replace it with more recent version. In the pom.xml
, you would then have:
<properties> <spring.version>3.1.0.RELEASE</spring.version> <hibernate.version>3.2.7.ga</hibernate.version> <asm.version>3.1</asm.version> </properties> ... <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> <version>${hibernate.version}</version> <exclusions> <exclusion> <groupId>asm</groupId> <artifactId>asm</artifactId> </exclusion> <exclusion> <groupId>asm</groupId> <artifactId>asm-attrs</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>${asm.version}</version> </dependency>
Other improvements
I took the opportunity to upgrade Spring 2.5 to Spring 3.1 (cf the properties above).
Besides, I modified the *.hbm.xml files to use object types, rather than primary types, eg replacing:
<id name="jonathanId" type="long">
with:
<id name="jonathanId" type="java.lang.Long">
(long tweet) Failure to find javax.transaction:jta:jar:1.0.1B
Case
Getting and building a project got on the internet, I got the following stractrace:
[ERROR] Failed to execute goal on project skillsPoC: Could not resolve dependencies for project lalou.jonathan.poc:skillsPoC:war:1.0-SNAPSHOT: Failure to find javax.transaction:jta:jar:1.0.1B in http://192.168.0.39:8081/nexus/content/repositories/central/ was cached in the local repository, resolution will not be reattempted until the update interval of localRepository has elapsed or updates are forced -> [Help 1] [ERROR]
Actually, the needed JAR (javax.transaction:jta:jar:1.0.1B
) is depended on by Spring 2.5.
Quick fix
- Add the following repository in your
pom.xml
:<repository> <id>java.net.m2</id> <name>java.net m2 repo</name> <url>http://download.java.net/maven/2</url> </repository>
- Unlike what I could read on Padova’s JUG blog, you need not get and install manually the jar any longer.
- Possibly, you may have to disable the block related to the
<mirrors>
in yoursettings.xml
.