Syncing web project dependencies maven -> eclipse - eclipse

I've got m2eclipse plugin installed and there's no problem syncing compile time dependencies that appear in pom.xml.
Now, I add JPA + Hibernate(or whatever) dependency that maven catches up just fine when doing mvn jetty:run.
The problem is, Eclipse doesn't. So I don't get those extra .jars deployed on the referenced tomcat when I launch the web app from eclipse.
Is there a way to add this kind of web runtime dependencies to Eclipse? Don't really like the idea of manually editing org.eclipse.wst.common.component.

I have found that any time you update a project's POM it helps to rerun m2eclipse or it will not see the new external dependencies.
If you have not tried to run this since you added your new dependencies ( JPA + Hibernate or whatever ) to your POM then give it a shot and see if it works.
mvn clean install eclipse:clean eclipse:eclipse eclipse:m2eclipse
Please note that after you execute this, you will probably need to refresh your Eclipse projects for the changes to take effect.
It might not require all of these arguments to work, but I sort of added them all at one point just to make certain. You can tweak the list of arguments to get it to the point that it works for you.
Doing this may blow away some of your Eclipse preferences for your project, so hopefully you have everything needed to rebuild your project already in your POM. For instance, I have a bunch of extra things in one of my POMs to ensure that things like springnature and springbuilder are added to my eclipse project when maven is used to rebuild the eclipse project.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<additionalProjectnatures>
<projectnature>
org.springframework.ide.eclipse.core.springnature
</projectnature>
<projectnature>
org.eclipse.wst.common.project.facet.core.nature
</projectnature>
<projectnature>
org.eclipse.wst.common.modulecore.ModuleCoreNature
</projectnature>
<projectnature>
org.eclipse.jem.workbench.JavaEMFNature
</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>
org.eclipse.wst.common.project.facet.core.builder
</buildcommand>
<buildcommand>
org.eclipse.wst.validation.validationbuilder
</buildcommand>
<buildcommand>
org.springframework.ide.eclipse.core.springbuilder
</buildcommand>
</additionalBuildcommands>
<wtpversion>1.5</wtpversion>
</configuration>
</plugin>
</plugins>
</build>
This last bit was added as a warning! Would hate for someone to take a working Eclipse project and destroy a bunch of the configuration if they were not already prepared for the consequences of running the eclipse:* commands.

I have found other solution. In my case there is WAR which depends on some JARs. Some JARs come from dependant projects (and they are deployed), and some are just plain dependencies (hibernate, spring, etc.).
I've spent few hours trying to determine why JARs from dependant projects are deployed and other - don't.
My solution was to create WEB-INF/lib folder in my WAR project! Usually you don't need them, because Maven2 creates this lib during package phase. In eclipse this is necessary.
My Eclipse is 3.6M4 and m2eclipse is 0.9.9.200912160759.

Related

Eclipse plugin with Maven dependencies for third party libraries

I am converting Subclipse to build with Eclipse Tycho and Maven.
Subclipse depends on a few third party JAR files that are not Eclipse plugins so do not exist in any p2 repository. Currently, I just include these in a lib folder within the plugin.
Since these JAR files do exist in Maven, I was hoping that by converting the plugins to build with Maven I could use Maven dependencies. IOW, the plugin would have a pom.xml where I used Maven dependencies to grab and include the third party jar's that have to be included in the plugin. Basically, it would just automate having to refresh what I include in the lib folder of the plugin.
Is this possible? I tried doing what I said above by when I build, I saw no sign that Maven/Tycho was trying to fetch the dependencies. I imagine it is because when the packaging is eclipse-plugin it looks solely at the Eclipse configuration files for the dependency information.
Thanks
To add plain (without OSGi metadata) jar files into your folder at biuild time, you can specify an <execution> of the maven-dependency-plugin to fetch them. However it will require to update your MANIFEST.MF Bundle-Classpath directive whenever a version changes.
It's usually better to hunt for OSGi-able jars or to make an extra effort to package existing libs as OSGi bundles/p2 artifacts like Eclipse Orbit or JBoss Tools Locus do.
Did you try doing the following after adding the dependencies to the pom.xml file?
Project->Clean
Right click on project: Maven->Update dependencies
Right click on project: Maven->Update project configuration
Just adding the plugin to pom dependencies and including the entry <pomDependencies>consider</pomDependencies> in the configuration of target-platform-configuration makes it work.
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho.version}</version>
<configuration>
<!-- The configuration to make tycho consider the maven dependencies -->
<pomDependencies>consider</pomDependencies>
<!-- other configurations -->
</configuartion>
</plugin>
<!-- other plugins-->
</plugins>
<dependencies>
<!-- An example third-party bundle (plugin) present in maven repository-->
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.gogo.shell</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
Reference link here.

How to run the project after building with maven

I am new to maven. So I have a project with pom.xml file. So I ran that with maven and the build was successful. I have glassfish. Glassfish is already running separately. So now what is the next step to run the project with Glassfish? My IDE is eclipse.
You have to first tell Maven to build the WAR, check out this plugin for that: http://maven.apache.org/plugins/maven-war-plugin/.
Then you need to tell maven how to deploy to glassfish, you can either configure a Maven execution plugin to do this (see here: https://www.mojohaus.org/exec-maven-plugin/). Or you can look around for a custom plugin devoted to integrating maven with glassfish. This one looks promising, but I have not used it: http://maven-glassfish-plugin.java.net/.
Maven provides a lot of basic functionality out of the box, but most of the cooler stuff with build automation is done through plugins.
Update
Just updating to add a very simple Pom that will do a auto-deployment. Note: if you just run a "mvn clean install", with the packaging set to 'war', maven will build the .war file for you and place it in the target/ folder. You can take this and deploy it to glassfish manually if you just want to get started.
Below is part of a very simple pom that uses the Maven execution plugin to auto-deploy to glassfish as a function of the build:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
<phase>install</phase>
</execution>
</executions>
<configuration>
<executable>${path-to-asadmin-util}</executable>
<arguments>
<argument>deploy</argument>
<argument>--user=${username}]</argument>
<argument>--passwordfile=${password-file}</argument>
<argument>--host=localhost</argument>
<argument>--port=4848</argument>
<argument>target/${project.name}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
This basically just calls the deploy command on the glassfish asadmin utility[1]. You need to fill in the following variables:
${path-to-asadmin-util} --> this is the path to your asadmin utility
(normally in the glassfish_home/bin)
${username} --> glassfish admin username
${password-file} --> password file for logging into glassfish
admin[2]
${project.name} --> name of your war
If you want to get more complicated I suggest taking a look at this thread: GlassFish v3 and glassfish-maven-plugin (Mac).
[1] - http://docs.oracle.com/cd/E18930_01/html/821-2433/deploy-1.html#SJSASEEREFMANdeploy-1
[2] - http://docs.oracle.com/cd/E18930_01/html/821-2435/ghgrp.html#ghytn
Additonnaly, you should have a glance at this StackOverflow thread, dealing with maven deployement in glassifsh : https://stackoverflow.com/a/1836691/1047365.
For further understanding of Maven, you should REALLY read this (free) book : http://www.sonatype.com/books/mvnref-book/reference/. This is THE reference for Maven.
We can explain you what Maven is doing, producing, etc ... but Sonatype made a great work and you'll probably learn more reading it than we could ever do !
Regards.
I found this tutorial useful: http://tshikatshikaaa.blogspot.com/2012/05/introduction-to-maven-concepts-crash.html

Maven project not being treated as Java in Eclipse

I'm working on a project that has lots of different Maven projects. I've been doing a bunch of JUnit testing on these projects, and usually this goes well. I open up Eclipse, right click in package explorer->Import... Existing Maven Projects and I'm able to import the project fine. I can build, drill down to src/test/java... Right click on the file and do a Run As JUnit test. Every now and then though, I can't get this to work. If I right click to do a Run As, all I get is AspectJ/Java application. There's no JUnit tests.
I noticed that the icon next to the project folder only has an M and a folder icon, whereas with projects that do work, there's a folder, M, AND a AJ. I've also noticed that it doesn't seem to sort the files into their packages like normal Java projects. It seems like it's not treating the project as an AspectJ project. How do I get Eclipse to recognize this Maven project as a Java project?
Several of the existing answers should work, but those suggesting to add a Java facet will only work if your project already is of (or you want to convert it to) a faceted nature, and the one suggesting you change your pom.xml will only work if you then re-import your project as a maven project.
If you'd like to "fix" your existing project, i.e. add a Java nature without converting it to faceted form and without deleting and re-importing, just edit your .project file (externally, or with the Navigator view in eclipse, as it won't show up in your package explorer) and add the java builder and java nature to the existing maven builder/nature:
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>yourprojectname</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<!-- add this build command -->
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<!-- this would've already been there -->
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<!-- add this nature -->
<nature>org.eclipse.jdt.core.javanature</nature>
<!-- this would've already been there -->
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
Note the different capitalisation (javabuilder but maven2Builder, javanature but maven2Nature).
As soon as you save, it should re-build automatically. You may have to manually add any source folders (Project properties -> Java Build Path -> Source tab -> Add Folder....
Right-click on Project -> Properties -> Project Facets, then choose all facets that apply for your case (e.g. java).
mvn eclipse:eclipse will create necessary .project files which will add java nature & other properties to the project
I was facing the same problem and the steps below solved it in my case:
1- Open the pom.xml of the problematic project.
2 -In the overview tab, check the Artifact/Packaging settings.
3- If it is set to "pom" change it to "jar".
4- Save your changes and then delete the project from eclipse only and re-import it.
Hope that helps!
It depends on the Eclipse project facet properties (if you are using Java EE), I suggest you right click on project properties and see which facet is defined for your project.
I faced the same problem(after importing pom.xml to eclipse,the project is not treated as a java one,and there is only "maven project builder" in project builders property,but no "java builder") in eclipse luna.
Re-importing maven project after deleting ".settings" folder and ".project" file did not work.
Re-importing after a workspace switch worked for me.
I talked to a co-worker and I was able to "fix" the problem. I did a delete from Eclipse (not from disk) and immediately re-did the Maven import and now it magically works.
It seems like if there was an error with the pom.xml, particularly if the parent version was wrong, that the maven project doesn't get imported/created properly. Once I fixed the problems in the POM, the project would build fine without any problems but it was still only a Maven project. Once I removed it and re-imported it, THEN it treated it as a Maven/AspectJ project.
Check the pom.xml file for projects that don't get the AspectJ nature (AJ on the project). Are they missing the AspectJ-Maven plugin? They should have a section like:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<!-- use this goal to weave all your main classes -->
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<complianceLevel>1.6</complianceLevel>
</configuration>
</plugin>
Also take a look at his question: Maven/AJDT project in Eclipse
There are two ways this can be achieved.
1) By Changing the project nature.
Right-click on Project -> Properties -> Project Natures, then choose all natures that apply for your case (e.g. java).
(OR)
2) By updating the .project file and add all the natures that are required for the project. This .project file is located at the root folder of the project. If the file does not exist for any reason, create a new file, and add the sections for each nature that is required e.g., as follows.
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>yourprojectName</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
I know that this question was a long time ago...
I just resolved the issue by these 2 steps below.
run cmd "mvn eclipse:eclipse" in the root directory of the project
right-click on the project in eclipse then select "import as project"
My five cents...
In my case I was 'losing' always the 'Java' nature of the project because the pom file was set to 'package as ear'. There was no error or warning message though, which would be nice to have. As soon as I changed the package to 'jar' I could set the nature of the project to Java and it is persisted.
I also tried all the approaches described in this thread. Modifying only .project is not enough because the maven libraries will not be used for building the project and the project build will fail. mvn eclipse:eclipse will create a long and useless .classpath without actually using m2e.
The easiest way is to create a new java project, convert it to the Maven project and open the .project and .classpath for this new project. Then make similar changes for the imported Maven project.
The important changes for the .project file have been described but in order to use the Maven dependencies in Eclipse something like this should be in the .classpath file
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
Using facets is not an option in this case because with facets Eclipse doesn't use m2e and the maven dependencies. Again the dependencies with facets have to be managed manually.
I tried all this with Eclipse 4.6.2 Neon.
I had the same problem. In my case it was some defect in eclipse workspace. I recreated it (deleted .metadata directory) and that fixed the issue.
If you have tried Amos M. Carpenter's solutions on the .project and didn't work, go to Project -> Properties -> Project Nature and Add... Java, Apply and Close button.
Nothing above helped me. After hours of trying (shortly before crying) I imported the projects not by "Import from existing maven projects" but instead with "Projects from Git".
Everything is fine now. The Java Projects are Java Projects again!
Right click the project and click on the Properties --> Select Maven --> (In active maven profiles) give "pom.xml", Apply and close. enter image description here
Go to u r project folder and delete .settings
and rebuild the maven application
then try with running project
Good Luck

Maven dependency and eclipse classpath

I tried to search for existing questions but cant find any - feels like my question is quite simple but probably because it's quite specific I cant find the answers on Stackoverflow / Google.
Anyways - I have few projects with Maven that are depend on each other. In certain cases I want the dependency to be on the JAR rather than a project dependency (ie. I want the dependency to be part of the "Libraries" in Eclipse rather than "Projects" in the Build Path).
Your help is greatly appreciated! Thanks
To get the referenced projects in the same workspace as jar files instead of the projects, we could use the VM parameter -Declipse.useProjectReferences=false or add it in the pom file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<useProjectReferences>false</useProjectReferences>
</configuration>
</plugin>
See this URL for more info.
In your project properties in Eclipse, select Maven. There is a checkbox, 'Resolve dependencies from workspace projects'. If this is checked, then the Eclipse projects are used. Otherwise the jars are used as with other maven dependencies, assuming that you've got the dependencies in your pom.xml as normal.
EDIT: If your project is not a maven project, then you'll have to create the jar outside Eclipse and add it as a jar or external jar as normal. If the project is a maven project, then the above will work.
Say Client-Project depends on Services-Project. If Services-Project generates a JAR. In the Client-Project POM you would express a dependency on this JAR. It would be something like:
<dependency>
<groupId>group.id.of.services.project.goes.here</groupId>
<artifactId>artifact.id.of.services.project.goes.here</artifactId>
<version>version.number.of.services.jar</version>
</dependency>
If services project generates a JAR called com.mycompany.services-1.3.jar, the dependency would be:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>services</artifactId>
<version>1.3</version>
</dependency>

Why does the maven eclipse plugin break the maven jetty plugin?

I would like to have the maven eclipse plugin regenerate my .classpath whenever a build is run, and I did so by using the following configuration:
<!--
Generate a new .classpath each time the build is run, but don't try
to download sources or javadocs
-->
<profile>
<id>elipse-update</id>
<activation>
<file>
<exists>.classpath</exists>
</file>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>eclipse</goal>
</goals>
<configuration>
<downloadSources>false</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
For some reason, this causes the maven jetty plugin to fail with ClassNotFoundException errors (it complains about all sorts of Spring classes not being there). Of course, it worked without a hit when I didn't have the maven eclipse plugin installed. Here's an example of what I'm talking about:
$ mvn jetty:run
[INFO] Scanning for projects...
...
[INFO] Starting jetty 6.1.22 ...
2010-02-11 20:53:08.984:INFO::jetty-6.1.22
2010-02-11 20:53:09.109:WARN::Could not instantiate listener org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at org.codehaus.classworlds.RealmClassLoader.loadClassDirect(RealmClassLoader.java:195)
at org.codehaus.classworlds.DefaultClassRealm.loadClass(DefaultClassRealm.java:255)
at org.codehaus.classworlds.DefaultClassRealm.loadClass(DefaultClassRealm.java:274)
at org.codehaus.classworlds.RealmClassLoader.loadClass(RealmClassLoader.java:214)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
Of course, if I delete that eclipse plugin section, I can run jetty as expected:
$ mvn jetty:run
[INFO] Scanning for projects...
...
Feb 11, 2010 8:55:28 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 1672 ms
2010-02-11 20:55:28.687:INFO::Started SelectChannelConnector#0.0.0.0:8080
[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 5 seconds.
Some things I know:
Yes, I only activate this profile if the .classpath is present. This seems counter-intuitive, but I have a reason: I have another profile that activates when the .classpath is absent which runs the eclipse plugin with the options for downloading source code and javadocs set to true. I don't want this to happen each build, so I created a separate plugin config for when the classpath is already there.
Yes, I could simply create properties that held the values of the options I wish to change instead of specifying the entire plugin config again. In that case, I would just set a eclipse.downloadSources property to true or false depending on the presence of the classpath and have a single plugin definition in the regular build section.
Any advice? This is a strange problem.
Thanks,
LES
I suspect the Maven Eclipse Plugin to mess do some classpath woodo that affects the jetty plugin later.
But to be honest, this is not a very common way to use the Eclipse plugin, at least not to my knowledge. Most people don't include it in their build, they just run it when the POM has changed and this generally doesn't happen every build. Moreover, touching the .classpath confuses Eclipse if I remember well and forces a clean rebuild of the project which is pretty annoying.
So at the end, and I'm sorry for that, this seems to introduce more annoyances than benefits. You can try to open a Jira issue though.
You really don't want to run the maven-eclipse-plugin on every build when there's a .classpath present. I can't tell you exactly what it does to the classpaths, but this is not how it's intended to be used. The assumption is that when you run it you only run eclipse:eclipse (or some other goal).
Why do you want to keep re-running it?