Maven Dependency error in Eclipse - java

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?

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.

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.

Related

Is it possible to turn off (prevent activation) of a maven profile with gradle? - (unwanted maven profile active)

I have an issue with the barcode library zxing.
compile('com.google.zxing:core:3.4.1') compile('com.google.zxing:javase:3.4.1')
The com.google.zxing core-3.4.1.pom is refering to a parent:
<parent>
<groupId>com.google.zxing</groupId>
<artifactId>zxing-parent</artifactId>
<version>3.4.1</version>
</parent>
..and the zxing-parent-3.4.1.pom contains this snippet
<profiles>
<profile>
<id>build-android</id>
<activation>
<property>
<name>env.ANDROID_HOME</name>
</property>
<jdk>[,9)</jdk> <!-- Android won't necessarily work with JDK 9 -->
</activation>
<modules>
<module>android-core</module>
<module>android-integration</module>
<module>android</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>${android.platform}</version>
<scope>system</scope>
<!-- ANDROID_HOME must be absolute, but redundant leading / may help Gradle Spring Boot plugin -->
<systemPath>/${android.home}/platforms/android-${android.platform}/android.jar</systemPath>
</dependency>
</dependencies>
</dependencyManagement>
</profile>
<profile>
Now the missing systemPath leads to this error:
Task :compileJava
Errors occurred while build effective model from C:\Users\eXtensia.gradle\caches\modules-2\files-2.1\com.google.zxing\core\3.4.1\d1e7c4667f9dcdda30d09f3df7d1b44f65c44336\core-3.4.1.pom:
'dependencyManagement.dependencies.dependency.systemPath' for com.google.android:android:jar must specify an absolute path but is ${env.ANDROID_HOME}/platforms/android-22/android.jar in com.google.zxing:core:3.4.1
Errors occurred while build effective model from C:\Users\eXtensia.gradle\caches\modules-2\files-2.1\com.google.zxing\javase\3.4.1\357121cdf3f89f63ddfaad18170612dcad0db947\javase-3.4.1.pom:
'dependencyManagement.dependencies.dependency.systemPath' for com.google.android:android:jar must specify an absolute path but is ${env.ANDROID_HOME}/platforms/android-22/android.jar in com.google.zxing:javase:3.4.1
Question
Gradle: is it possible to turn off this profile with gradle as android is not needed?
Gradle: Other ways to manipulate the downloaded .pom automatically via gradle to remove this profile?
Maven: Suggestion how to change the zxing-parent-3.4.1.pom that it will continue to run for android users, non android users and maven/gradle users? (there is already an issue and there are chances to get it changed.)

Maven Tomcat7 run a dependency war

I have a question regarding maven and its tomcat7 plugin :)
I have the following maven projects:
plugin1: plain java project packaged as jar
plugin2: plain java project packaged as jar
webapp: standalone webapp project packaged as jar
those three project are properly build in maven and the outcome works fine:
I can use the jars from plugin1/plugin2
I can deploy the webapp war file to a web container
I can run tomcat7:run to start the webapp
Now, I need to provide different packaging of the webapp containing specific plugin setup.
i.e. I want to generate a war file with webapp + plugin1 and another one with webapp + pugin2
To achieve this, I have created 2 additionnal maven projects that declare dependancies on the webapp project + the appropriate plugin projects and are packaged as wars.
The generated war files have the expected content, and can be deployed to a tomcat, but when I try to use the maven tomcat plugin (tomcat7:run again), it simply doesnt start anything.
Though this is not blocking for me (my main point was to generate the war files), I have the feeling that I missed something.
the pom.xml for those aggregate projects looks like this (note that there is absolutly no code in those projects, these were just created for packaging with specific dependancies convenience).
<groupId>my.project</groupId>
<artifactId>live1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>MyWebapp</name>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<dependency>
<groupId>my.project</groupId>
<artifactId>plugin1</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>my.project</groupId>
<artifactId>webapp</artifactId>
<version>${project.version}</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<finalName>MyWebapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<url>http://localhost:8080/manager</url>
<server>localhost</server>
<path>/${project.build.finalName}</path>
</configuration>
</plugin>
</plugins>
</build>
Thanks !
note: long time lurker, first time asker here, if some information is missing tell me :)
Depending on the structure of your project it may not be suficient to just add a dependency of type war. You may need also to configure <overlays> as described here maven-war-plugin.
It looks like your final war does not provide the full web configuration that you expect. With overlays you can configure how the resources from the dependency will be packed into your final web app.
There must be some difference in the way that your external tomcat starts the app compared to the tomcat7 plugin. May be you can try -X option :
mvn -X tomcat7:run
This should log out some details, of what the embedded tomcat is configuring..

Customised maven deployable (war with some dependencies removed)

I have a multi module web app building with maven. We build the war as per normal and deploy and run on developer machines and local test servers using tomcat.
Then we want to deploy the application to the cloud. To do this we create a special version of tomcat which has all the libraries preloaded and a special version of the war which only has our code. Point here is tomcat is preloaded on the cloud server, the war is uploaded each time it is changed. Currently we are having to manually remove the dependencies from the built war.
What is the best way for maven to do this? Should I build a custom packaging type or maybe run some post build plugin to remove these wars? Or something else? I think the best way to activate this custom build is via a profile. I did try and remove these dependencies by setting them to scope = provided in the new profile but the transitive dependencies still made it into the war.
If you want to exclude all dependencies, you can use the war plugin's packagingExcludes to do so:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
...
</configuration>
</plugin>
Specify this plugin inside a profile to only perform it for production.
You can achieve using profile in maven. As you said it is not working, I can think of you configure something wrong. Try something like:
<profiles>
<profile>
<id>dev</id>
<activation>
<!-- active by default, turn off when on prod -->
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<!-- include this in dev, not in prod -->
<dependency>
<groupId>com.company</groupId>
<artifactId>xyz</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
Then in command line, mvn package -P !dev to deactivate dev profile so that not include the jars.
Make sure com.company:xzy is not included in <project><dependencies></dependencies></project>.

Plugins in Maven and POM.xml

I just started using Maven and I read that plugins are additional components that can be used.
A typical structure of pom.xml file is
<project>
<groupId>org.koshik.javabrains</groupId>
<artifactId>JarName</artifactId> (A fldernamed JarName was created)
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>JarName</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Question: Where should I insert a plugin tag? such as the following:
<plugin>
<groupId>org.jibx</groupId>
<artifactId>jibx-maven-plugin</artifactId>
<version>1.2.4</version>
<executions>
<execution>
<goals>
<goal>bind</goal>
</goals>
</execution>
</executions>
</plugin>
Before the dependency or after the dependency tag? Does it matter?
<project>
<groupId>org.koshik.javabrains</groupId>
<artifactId>JarName</artifactId> (A fldernamed JarName was created)
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>JarName</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.jibx</groupId>
<artifactId>jibx-maven-plugin</artifactId>
<version>1.2.4</version>
<executions>
<execution>
<goals>
<goal>bind</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
You can also place plugins in the <build> section of <profile> if you use maven profiles. The order doesn't matter.
A late clarification on two important points
Where to place plugin
A plugin should indeed be added in most of the cases within the build/plugins section, however there is an important difference between placing it within plugins against placing it within pluginManagement/plugins.
This misunderstanding is often the cause of a non invoked plugin in Maven or an harder troubleshooting:
Plugins under build/plugins are directly part of the default Maven build, if they specify an execution or if they configure something for the default build (see below)
Plugins under build/pluginManagement/plugins are not necessarely part of the default Maven build, that is, is a management, it's an hint to maven: it you happen to use this plugin, then please use the version, the configuration, the executions I specify here, in this management.
But what happen to use means? Means: if the same plugin is also present in the build/plugins section, then apply this management (and only then it will be effective); or if the plugin is invoked by default by Maven, then also apply it.
But how is a plugin invoked by default? That's part of the main philosophy behind maven: convention over configuration. By convention, when you specify a certain packaging (default jar, but it can be war for example), you want certain plugins to be invoked. To build a jar, by default invoke the maven-jar-plugin; to build a war, by default invoke the maven-war-plugin and so on. So, if you specify a plugin configuration in the build/pluginManagement/plugin for a plugin which has a default binding to the Maven build, then it will be also be used.
Ordering
Concerning the ordering of sections within the pom.xml file, a further clarification is required: it's indeed irrelevant in most of the cases, however the order of plugin element wihtin the build/plugins section may be important. Since Maven 3.0.3 (MNG-2258), different plugin executions attached to the same Maven phase will be invoked in their order of declaration in the pom.xml file. That is, ordering is important in this case, since it may affect the behavior of the build.
Additionally, also order of dependency declarations may affect your build towards Dependency Mediation, that is, the first declared dependency wins in case of conflict against a transitive dependency. So, once again, ordering is important in certain cases.
Last but not least, although ordering is not important for other sections of the pom.xml file, good habit is to follow official Maven recommendations and, as a simplified version, follow this order of declaration:
<project>
<modelVersion/>
<parent/>
<groupId/>
<artifactId/>
<version/>
<packaging/>
<properties/>
<dependencyManagement/>
<dependencies/>
<build/>
<reporting/>
<profiles/>
</project>
The sortpom-maven-plugin can also be used to automatically apply this standard ordering, simply invoking the following on the concerned pom.xml file:
mvn com.github.ekryd.sortpom:sortpom-maven-plugin:2.5.0:sort \
-Dsort.keepBlankLines -Dsort.predefinedSortOrder=recommended_2008_06
For further reading:
Stack Overflow: Maven: what is pluginManagement?
Official Maven doc: Maven POM Reference, PluginManagement
Official Maven default bindings
Official Maven doc: Dependency Mediation
Official Maven doc: Maven Code Style And Code Conventions
<plugin>should be placed into <plugins> section which should be placed into <build> or <pluginManagement> section.
The order of <dependency> or <build> section doesn't matter.
The full reference about pom.xml is here: http://maven.apache.org/pom.html
If you want to use the plugin for build you can use the below structure.
<project>
<build>
<plugins>
</plugins>
</build>
</project>
You can insert your second snippet anywhere in the pom.xml file between two <plugins> </plugins> tags.
Sections order in POM doesn't matter. In general, there are build plugins and reporting plugins in Maven. Your case is to use build plugin so you have to put this <plugin> block into <project><build><plugins>... section.
Look at this for some basics about plugins.
As additional answer for Reporting Plugins (e.g. maven-checkstyle-plugin) there are 2 tags under which plugins can go in pom.xml, under build and reporting.
Using the reporting Tag VS build Tag
Configuring a reporting plugin in the or elements
in the pom does NOT have the same behavior!
mvn site
It uses only the parameters defined in the
element of each reporting Plugin specified in the element,
i.e. site always ignores the parameters defined in the
element of each plugin specified in .
mvn aplugin:areportgoal
It uses firstly the parameters defined in the element
of each reporting Plugin specified in the element; if a
parameter is not found, it will look up to a parameter defined in the
element of each plugin specified in .
Source: https://maven.apache.org/guides/mini/guide-configuring-plugins.html#Using_the_reporting_Tag_VS_build_Tag

How to add reference of Mavenized Projects

I have two mavenized projects
Project_A: Java Project
Project_B: Java Web Project
Earlier, before mavenizing both projects, i had refrence(buildpath) of Project_A in Project_B.
Now i have configured both projects with maven2. MY questions is, how can i add a refrence of Project_A in Project_B that WAR of Project_B can be runnable
so far i have tried following chunk of code which seems to work but on runtime Project_B's WAR throw an exception in applicationContext that an Instance of Project_A's class cannot be initiated.
1- Project_A is cleaned and installed in local repository with name "myproject-1.0.jar"
where build snippet(Project_A's POM) is as follows#
Edited Part
<modelVersion>4.0.0</modelVersion>
<groupId>org.myurl<groupId>
<artifactId>projectA</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>${project.artifactId}-${project.version}</name>
build>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
<testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${basedir}/src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<optimize>true</optimize>
</configuration>
</plugin>
</plugins>
</build>
Snippet of Project_B's POM, where i am adding installed jar of Project_A
<dependency>
<groupId>org.myurl</groupId>
<artifactId>projectA</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
Thanks for suggestions in advance.
Answer :
Actually there was not any problem with maven. Above configurations in POM are absolutely correct. I had moved properties files under /Resources and later i have forgotten to change paths in those classed where properties files are utilized.
Nevertheless i am thankful to Dolphin and Nicola Musatti for their suggestions.
You are on good way, pom snippet is fine what you need to do now is make parent project (Maven Reactor). So you will make new project and in that project define modules (Project A and Project B). You will reference that parent in Project A and Project B, and that will do the trick. Google maven parent to see how it's done.
After building Project B, maven will first build Project A and after that Project B.
Thing is your approach should work as well (just it's not a proper way to do it). So probably you are making mistake in groupId, version. You can check this by build Project A and then checking your local maven repository. Inside of it you should see folder structure if not something went wrong.
org/myurl/projectA-1.0.jar
Project strucuture is following:
project-parent
project-a
project-b
in project parent you define:
<groupId>foo.bar</groupId>
<artifactId>project-parent</artifactId>
<version>00.01-SNAPSHOT</version>
<name>FooBar:: Parent Project</name>
<packaging>pom</packaging>
<modules>
<module>../project-a</module>
<module>../project-b</module>
</modules>
in project-a and project-b you add:
<parent>
<groupId>foo.bar</groupId>
<artifactId>project-parent</artifactId>
<version>00.01-SNAPSHOT</version>
</parent>
In project-b you will have reference to project-a
<dependency>
<groupId>foo.bar</groupId>
<artifactId>project-a</artifactId>
<version>00.01-SNAPSHOT</version>
</dependency>
Once you build parent it should build all modules correctly. Don't use plugin, forget I said Maven Reactor :)

Categories

Resources