Integrate Maven with existing Java Project using Eclipse - eclipse

I have created this Java project using struts, hibernate in Eclipse Helios,
Now I want to integrate this project with Maven how to do it?
I have installed Maven In Eclipse already.
Tutorials, blogs, websites that I have found so far are explaining integration of some project into Maven outside Eclipse and then importing it in Eclipse or crreation of New project with Maven.None of them so far addressing my Problem.
As I mentioned I have created a project in Eclipse already Now I just want to integrate it with Maven, how to do it?

In eclipse you can easily convert a java project in a maven one right clicking on project -> configure -> convert to maven project.

While an IDE "importer" can sometimes be handy, it is not required to turn a project in Eclipse into a maven project. Basically all you just need is to add a pom.xml file and follow maven's conventions - or configure it.
By using the maven-eclipse-plugin it is actually possible have a maven itself generate the necessary files to integrate your maven project with eclipse:
Start from the command line
Go to your project's root
Create a new pom.xml file from a simple template or initiate a new project folder structure (including a pom) using mvn archetype:generate
Type mvn eclipse:eclipse.
Then maven has generated the necessary files to integrate with eclipse.
That said, maven by convention expects a certain folder structure of your Java project. It looks like this:
my-app
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- mycompany
| `-- app
| `-- App.java
`-- test
`-- java
`-- com
`-- mycompany
`-- app
`-- AppTest.java
So unless you already have this structure, you need to move your source code to main/java (and unit test code to test/java).
Further if your project has dependencies to other projects; then you need to express these dependencies in Maven's pom.xml file. If your dependency projects are stored in the Maven Central this is particularly easy. To express a dependency to e.g. Apache Commons - you would add this to your pom.xml:
<project>
...
<dependencies>
...
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
...
</dependencies>
...
</project>
After these initial attempts to integrate your project with maven, you can try to build with mvn compile from either the command line - or using the m2eclipse plugin for Eclipse.

Related

Can maven treat WEB-INF\lib the way eclipse (and m2e) does?

I have a servlet/jsp web project which runs fine on eclipse and is exported as war fine (once I clean it that is). I mavenized the project deleting all of the dependencies from the WEB-INF\lib folder except a homebrew jar (the output of another project in the workspace). When I run the package maven goal I get messages for missing classes from this jar:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
#..... NOTICE THIS COMES FROM A CUSTOM JAR
[ERROR] /C:/path/DataServlet.java:[3,30] package xxx.java.helpers does not exist
Now this has been asked before and the most rigorous solution appears to be to create a local repo: Can I add jars to maven 2 build classpath without installing them? (NB: I am at maven 3).
I would like to avoid this - so is there any way maven will just stuff this jar to WEB-INF\lib in the war ?
Solutions that use some maven plugin to cp the contents of the WEB-INF\lib in the war are welcome - although I just have this feeling that there should be a solution that takes into account the "special" nature of this folder.
Observations:
Alt+F5 removes this line:
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
which corresponds to the "Web App libraries" in the Java Build Path. So not only maven refuses to take into account the WEB-INF\lib - it also breaks the build path of eclipse completely.
Related:
Maven: How to include jars in Eclipse, which are not available in repository?
Uses the maven eclipse plugin : update my classpath with an Eclipse User Library via the maven eclipse plugin - not compatible with m2e
How does the m2e eclipse plugin interact with eclipse? - apparently m2e checks the pom then calls the eclipse builders (hence the .classpath is read)
Eclipse maven-enabled web app references workspace projects, but those are not deployed when running Tomcat server
Deploying a Maven project with dependencies to Tomcat or Jboss running within Eclipse
Did you add this jar from WEB-INF\lib as a dependency like this:
<dependency>
<groupId>someGroupId</groupId>
<artifactId>someArtifactId</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/homebrew jar</systemPath>
</dependency>

Beginner's Guide to Setup Xuggler

To work with Xuggler you need xuggle-xuggler-5.4.jar. According to the people who made this, most users only need the above-mentioned JAR file. However, this is what they say about using Xuggler without Maven or Ivy:
Using Xuggler WITHOUT Apache Maven or Apache Ivy
What are you... stuck
in 2003? Anyway, if you insist on this, Xuggler's pre-compiled
binaries (including native versions) can be found here. Make sure that
xuggle-xuggler.jar and its dependencies are included in your Java
classpath. See the xuggle-xugger-*.pom file distributed with the
version of Xuggler that you use to find the (small) set of dependent
jars, and download them as well.
I downloaded the xuggle-xuggler.jar file for the latest version, 5.4 but I don't understand the pom file for it.
What dependencies is he talking about?
Next, how do I download these dependencies ?
Once I get these dependencies, how do I start working in Eclipse?
Update After Downloading Dependencies
I have the following directory structure:
xuggle-xuggler-5.4.jar is stored in E:\xuggle
the various xuggler dependencies are stored in E:\xuggle\xuggle-dependencies
Question:
How do I start working with Xuggler in Eclipse? What paths do I have to set and what values do these paths have?
The following files list the other jars which xuggle depends upon:
ivy.xml
pom.xml
You can read these and then manually retrieve them from the appropriate repository, but I would submit it's simpler to start using a dependency manager.
You asked how to download these dependencies, well ivy has a convenient command-line mode of operation. (See example below)
Eclipse integration is very tough.... Once you've downloaded the jar you could try and generate the ".classpath" file or just manually add each jar via the Eclipse GUI.
The reason I don't recommend this approach is because there are Eclipse plugins for both Maven and Ivy that would do this for you automatically.
Example
Run ivy from command-line as follows:
java -jar ivy.jar -settings ivysettings.xml -dependency xuggle xuggle-xuggler 5.4 -retrieve "lib/[artifact]-[revision].[ext]"
It will retrieve xuggle and all its dependencies into a "lib" directory as follows:
├── ivysettings.xml
└── lib
├── commons-cli-1.1.jar
├── logback-classic-1.0.0.jar
├── logback-core-1.0.0.jar
├── slf4j-api-1.6.4.jar
└── xuggle-xuggler-5.4.jar
ivysettings.xml
This file tells ivy to retrieve jars from either Maven Central, or the Maven repository provided by the Xuggle project.
<ivysettings>
<settings defaultResolver="repos" />
<resolvers>
<chain name="repos">
<ibiblio name="central" m2compatible="true"/>
<ibiblio name="xuggle" m2compatible="true" root="http://xuggle.googlecode.com/svn/trunk/repo/share/java"/>
</chain>
</resolvers>
</ivysettings>
Don't fight Maven, embrace it. These days all major build systems are maven compatible (Maven, Ivy, Gradle, Grape, Buildr ...). But you can use Maven from Eclipse:
create a file called pom.xml with this content:
<project>
<groupId>com.foo<groupId> <!-- change these -->
<artifactId>foo</artifactId> <!-- parameters to whatever -->
<version>1.0-SNAPSHOT</version><!-- you like -->
<repositories>
<repository>
<id>xuggle repo</id>
<url>http://xuggle.googlecode.com/svn/trunk/repo/share/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>xuggle</groupId>
<artifactId>xuggle-xuggler</artifactId>
<version>5.2</version>
</dependency>
</dependencies>
</project>
Install the m2e extension and, from Eclipse, do "File > Import ... > Existing Maven Projects". In the dialog, select the Folder that contains the pom.xml.
Make sure that the Folder's layout is like this:
pom.xml
src/main/java // sources go here
src/test/java // test sources go here
Then you should have a working Eclipse project with the required dependencies.
Update after your update:
You can see the dependencies when you look at this file: http://xuggle.googlecode.com/svn/trunk/repo/share/java/xuggle/xuggle-xuggler/5.2/xuggle-xuggler-5.2.pom
commons-cli (a utility library for command line processing)
logback (a logging framework)
junit (a testingframework)
Maven will take care of loading these dependencies for you. So will Eclipse, if you use the m2e plugin as suggested above.
If you absolutely don't want to do that, you will have to download the dependencies manually. Look at the pom file above, note the names and versions of the dependencies, look them up at http://mvnrepository.com/ and download them there, e.g. this is the page for slf4j-api: http://mvnrepository.com/artifact/org.slf4j/slf4j-api/1.6.4

Eclipse Maven Dependency

I have just added dependencies to an eclipse project so that my jar can see other jars. How can I be sure that the dependencies work, and that what I've done is correct? I view a pom.xml file that has been created so what are the target folder and classes,test-classes subfolders used for? Thanks
If you have the m2eclipse plugin installed you can open your pom in Eclipse and click on the Dependency Hierarchy tab to view your resolved dependencies. You should manage all dependencies through Maven with the setup you are describing.
If you want to check command line you may want to look at using Effective Pom.
If you use m2e, it adds Maven Dependencies pseudo library to your project.
You may expand it and see if the dependent jar file is in there.
If it is, Eclipse ( or more precisely m2e ) has resolved the dependency correctly and it's available for you project build.
If you added your dependencies correctly your application should build and execute correctly, or am I missing something? Dependencies should be added to a POM section that looks like this example:
<dependencies>
<dependency>
<groupId>annogen</groupId>
<artifactId>annogen</artifactId>
<version>0.1.0</version>
</dependency>
<!-- other dependencies here -->
</dependencies>
Maven and the m2e/m2eclipse plugin rely on source files to be conventionally placed in src/main/java for application code and src/test/java for test code. Application code is compiled to target/classes and test code is compiled to target/test-classes. If you plan to use Maven and/or m2e/m2eclipse, do read about it. Maven: The Complete Reference is a good starting point.

How to manage shared resources for several web apps in Maven AND Eclipse?

Note: I didn't get any response in the first version of this question, so I modified it to be more generic...
Context
My project is divided into several maven modules and several web-applications. Here is the structure:
my-project
+ pom.xml
+-- commons
+-- persistence
+-- ...
+-- web-app-1
+-- web-app-2
+-- ...
All the web applications share common resources, such as JS, CSS and images files.
Instead of duplicating these resources in each web-app-X, I decided to create another project called web-resources, which is a WAR project.
The structure is then the following one:
my-project
+ pom.xml
+-- commons
+-- persistence
+-- ...
+-- web-app-1
+-- web-app-2
+-- ...
+-- web-resources
+-- pom.xml
+-- src/main/webapp
+-- web.xml (which is almost empty, just need to be present for Maven)
+-- web_resources
+-- css
+-- images
+-- javascript
Maven
In Maven 2 (or Maven 3, as I just migrated my project to maven 3.0.2), this configuration is easy to manage as all web-app-X declare web-resources as a dependency:
<groupId>foo.bar</groupId>
<artifactId>web-app-1</artifactId>
...
<dependencies>
<dependency>
<groupId>foo.bar</groupId>
<artifactId>web-resources</artifactId>
<version>${preclosing-version}</version>
<type>war</type>
</dependency>
...
So when I build my WAR project, it first get the web-resources.war (built just before), unzip it, and build on top of it the web-app-X web-application.
This way, my WAR file will contains also a directory called web-resources/ that contains the shared resources.
This is the war overlay principle.
So on a Maven point of view, everything is fine!
Eclipse
Now, here comes the main problem: having a good Eclipse configuration.
Question: How can I use my current configuration to be managed correctly by Eclipse? In particular, when I deploy any web-app-X in Tomcat using Eclipse...
Note that I want to get the more automatizable (?) configuration, and avoid any manual steps, as this configuration should be used by dozens of developers...
For me, the best solution seems to use the linked resources of Eclipse. Thus, I set the following configuration in my web-app-X pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<wtpversion>1.5</wtpversion>
<linkedResources>
<linkedResource>
<name>web_resources</name>
<type>2</type>
<location>${project.basedir}\..\web-resources\src\main\webapp\web_resources</location>
</linkedResource>
</linkedResources>
</configuration>
</plugin>
...
When I run the mvn eclipse:eclipse configuration, it adds succesfully this information in my .project file:
<projectDescription>
...
<linkedResources>
<link>
<name>web_resources</name>
<type>2</type>
<location>C:\dev\project\web-resources\src\main\webapp\web_resources</location>
</link>
</linkedResources>
<projectDescription>
Now, I import my project in Eclipse.
Problem: in Project properties > Java Build Path > Source, I don't see the Link Source present.
I only see my four Maven default directories (src/main/java, src/main/resources, src/test/java and src/test/resources).
What is strange is that when I try to manually add the linked resources, it refuses and says that it already exists...
So when I deploy my web application on my Tomcat in Eclipse, it does not deploy the web_resources directory, and thus I don't get the CSS / JS / images deployed.
After some tests, it seems that I have to do two modifications:
Add the line <classpathentry kind="src" path="web_resources" output="src/main/webapp/web_resources"/> in my .classpath file;
Remove the <project>preclosing-web-resources</project> in the .project file.
Note that using this configuration, Eclipse will copy (and keep synchronization) the content of web_resources project in my web-app-X/src/main/webapp/web_resources, but this is not a problem (this directory is ignored by the SCM).
The only automated solution I found was to create a simple Maven plugin that do the two previous modification, and then run the following command (or use a .bat file):
mvn eclipse:clean eclipse:eclipse myEclipsePlugin:eclipse
Question
Is there a better way to manage such configuration?
Technical information
Java 6, Maven 3.0.2, maven eclipse plugin 2.8, Eclipse 3.3.2 (but I can test with newer version of Eclipse), no m2eclipse plugin.
Starting with Servlet 3.0, you can share resources by putting them within the src/main/resources/META-INF/resources directory.
When the webapp deploys, Servlet 3.0 makes those resources available from the context path. For example, in your case...
web-resources
-- src
---- main
------ resources
-------- META-INF
---------- resources
------------ css
-------------- global.css
------------ images
-------------- background.png
Let's assume that my_project has a dependency on web-resources and is deployed to the url http://my.localhost:8080/myProject.
With that configuration, the following URLs will resolve to the correct resources:
http://my.localhost:8080/myProject/css/global.css
http://my.localhost:8080/myProject/images/background.png
IF you have a name conflict between the resources and your actual application, the application will win.
Be sure your web.xml states you are using Servlet 3.0
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
You might want to take a look at this blog post. It covers a method for sharing resources using maven:
http://www.sonatype.com/people/2008/04/how-to-share-resources-across-projects-in-maven/

Cannot create a maven project from my archetype

I'm developing projects in Eclipse and I'm using maven, so I've created a new archetype, using the 'maven-archetype-archetype' Archetype, but once I try to create a new project with the my new archetype, I get the following error message in Eclipse:
The META-INF/maven/archetype.xml descriptor cannot be found.
Any idea what could the reason be?
Update:
My archetype.xml file:
<archetype>
<id>test-archetype</id>
<sources>
<source>src/</source>
</sources>
<testSources>
<source>test/</source>
</testSources>
</archetype>
My pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>$my.group.id</groupId>
<artifactId>$test-archetype</artifactId>
<version>$0.0.1</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The folder structure is as described:
archetype
|-- pom.xml
`-- src
`-- main
`-- resources
|-- META-INF
| `-- maven
| `--archetype.xml
`-- archetype-resources
|-- pom.xml
`-- src
`-- test
Adam.
I had exactly the same problem where Eclipse would report that the archetype.xml could not be found even though it did exist in the correct location. I did finally get it working after restarting Eclipse. It seems that if the archetype project is installed/deployed in the same Eclipse session as where it is used to create a project that it error.
The problem is that you (or Eclipse) would appear to be using "mvn archetype:create" command to create the new project which has been deprecated.
You should instead be using "mvn archetype:generate"
See http://maven.apache.org/archetype/maven-archetype-plugin/plugin-info.html
If you are using Eclipse try to use Eclipse IAM, it has simple wizard and ready solution for maven, and good integration with eclipse.
This appears to be a bug relating to Maven Repositories within eclipse. Maybe it is fixed in newer versions of eclipse.
Here is a work around:
When generating a new archetype, increment the archetype version (e.g. from 0.0.1-SNAPSHOT to 0.0.2-SNAPSHOT). After mvn install the new archetype version will appear in Workspace Projects, but not Local Repository.
Rebuild Index on the Local Repository. The new archetype version will now appear in Local Repository and is available for use.
I've tried this again recently, now that I'm much more experienced with Maven, and the result was the same, there is an issue with making archetypes, as far as I can see, perhaps Eclipse bug, or miss configuration, but whatever I tried, it didn't do the trick.