7

I have a war artefact and I need use some of their classes from a jar. I can't move the classes to another project, then I deploy the classes and resources included in my webapp as an "attached" artifact using the following configuration:

<plugin> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <attachClasses>true</attachClasses> </configuration> </plugin> 

This will result in two artifacts being deployed: mywebapp-1.0-SNAPSHOT.war and mywebapp-1.0-SNAPSHOT-classes.jar.

To use those classes I Referencing the artifact as follows:

 <dependency> <groupId>mygroup</groupId> <artifactId>mywebapp</artifactId> <version>${project.version}</version> <classifier>classes</classifier> </dependency> 

When I compiled from Jenkins everything works correctly, but when I run the tests locally from Eclipse can not find the reference classes. (java.lang.NoClassDefFoundError)

I think it might be a bug in the maven eclipse plugin, someone has any idea that can be happening?

2 Answers 2

5

Workaround is described on http://wiki.eclipse.org/M2E-WTP_FAQ:

A workaround exists though, we need to change the dependency whether the project is built in Eclipse or not. In your dependent project, you can configure the following :

<dependencies> ... <dependency> <groupId>com.company</groupId> <artifactId>mywebapp</artifactId> <version>1.0.0-SNAPSHOT</version> <classifier>${webClassifier}</classifier> </dependency> ... </dependencies> ... <properties> ... <webClassifier>classes</webClassifier> </properties> ... <profiles> <profile> <id>m2e</id> <activation> <property> <name>m2e.version</name> </property> </activation> <properties> <webClassifier></webClassifier> </properties> </profile> </profiles> 

The m2e profile is automatically activated when the project is built with m2e, ignored in other circumstances. In that case only, the dependent project will use an empty classifier to reference the web project, which will be added to the classpath as expected.

Sign up to request clarification or add additional context in comments.

2 Comments

I can confirm it solves problem with workspace resolution for dependencies with classifier; typically war projects with attachClasses=true. However, drawback is that now versions between dependency reference and referenced project must perfectly match, otherwise it is instaly reported as broken dependency. In practical term it means a lot more of git checktouts.
I'm having this problem when I have the dependent project opened in the workspace and try to add/remove the consuming project (that relies on the dependent war project) to Wildfly-10. If I have the project open in the workspace the <artifact>-classes.jar is not present in the deployed project, hence deployment fails. However, if I delete the project from the workspace the library IS included in the project and deployment is ok. I tried the workaround you described, but after reading into it, I don't think it really applies to my problem. Builds in eclipse also succeed without the workaround...
4

My simple answer is the following link to the bug tracking system of Eclipse:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=365419

See the answers inside.

Yes it's a problem with Eclipse itself..

The solution within Eclipse just add the project manually within your workspace to the appropriate project where you need the classes out of your war project.

1 Comment

Besides annoying necessity to manually fiddle with project setup, additional drawback is that classes and resources from dependent projects creeps into search results, so instead of opening source of referenced class you often end up in messy class file view or disassembled version of your own class you perfectly have available sources :( Note: classes got into results despite they are correctly marked as ‘derived’ resources, looks like a bug to me, verified on Eclipse Mars SR1.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.