I just started learning Maven. How does Eclipse build without it? - java

I've been coding Java in Eclipse for awhile without needing to specify dependencies. Now that I learned how Maven does it, I'm wondering: how did Eclipse build projects on its own? How did Eclipse figure out which versions of imports, and which dependencies of dependencies, are needed to make everything work?
Finally, what are the advantages and disadvantages of building a project in Eclipse by starting with New->Other->Maven Project instead of New->Java Project?

To the first question: Eclipse doesn't add any dependency in a standard Java project. You have to manually add to the Build Path all needed JARs, otherwise you'll have compilation errors.
To the second question: if you create a standard Java project Maven is not used, even if you create a pom.xml file in the root of the project. You can always convert a standard Java project in a Maven project (see Convert Existing Eclipse Project to Maven Project).

Your dependencies has always to be in the classpath
When you start a project with Maven, Eclipse will automatically add the Maven repository to the classpath.
When you start a Java project you have to link your library manually in Eclipse and the version of the library is the one you've downloaded.
You can see the difference in your project's Properties > Java Build Path > Libraries

Related

How do I add a java gradle submodule to my Android module?

Suppose I have an Android application project in IntelliJ Idea with Gradle. Call it MyApplication.
What I want to do is to add a plain java library module (not Android library project) as a submodule to MyApplication. Lets call this module a testlib.
And then when whole project is built, I want this module compiled to jar and included to /libs folder of MyApplication.
What I tried: I've created a submodule testlib and included it as a dependency to MyApplication, but I get a following warning:
Warning:Gradle: module 'testlib' won't be compiled. Unfortunately you
can't have non-Gradle Java module and Android-Gradle module in one
project.
Is this even possible?
If you've created your project in IntelliJ as an Android-gradle project, then when you go to the "Add new module" window you should see an "Gradle: Java library" option like this:
IntelliJ IDEA does not support Gradle and Non-Gradle modules in one single project. A comment from JetBrains in their forum states this clearly.
The solution is to either convert the Maven modules into Gradle
modules or split the project in two. With the latter approach, if you
build the plain java library module with Maven before building the
Gradle project, you will still be able to use it as a dependency in
the Android application project.
Beware of using different Java language source levels in multi-module
Gradle projects. IntelliJ IDEA does not handle them correctly due to a
bug in either IntelliJ IDEA or Gradle. (There is a pending discussion
whether this should be fixed in the IDE or Gradle.)

How can I convert a maven project to a Dynamic Web Project in Eclipse that builds with Ant?

I've got a java project that's using Maven in Eclipse, and would like to completely remove the Maven aspect of the project, and set it up as a completely stand alone Dynamic Web Project that doesn't depend on Maven at all, but uses Ant instead.
I assume this will involve:
finding out the libs that are required
creating an ant "build.xml" file
more steps that I'm not sure of ;-)
What's the best way to do this?
You could switch it to use ant. Maven comes with ant plugin. Run mvn ant:ant and it will generate the build.xml for you. This would make you dependent on ant though.
Probably the easiest thing to do is install m2e and m2e-wtp in Eclipse.
Then make sure your Maven project is packaged as a .war. See here for building a web app in Maven:
http://www.mkyong.com/maven/how-to-create-a-web-application-project-with-maven/
Eclipse will see it as a Dynamic Web App with all the configuration and deployment capabilities.

Maven/Eclipse plugin: easiest way to have new Maven project have dependency on legacy non-Maven project?

I created a new Maven project in Eclipse. This was working fine until I needed to add a dependency to another Eclipse project, a legacy utility project, which does not have a pom.xml, and does not have the directory structure of a typical Maven project. (It has the directory structure of a typical eclipse Java project). This other project is in the same Eclipse workspace as the Maven project.
In looking at other posts on this, it seems that usually the solution is to build the jar for the other project and install it in Maven. However I am actively modifying code in the utility project while writing code in the Maven project, so I can't just install a jar once to satisfy the dependency.
What is the easiest way to handle this so that I can code simultaneously in both projects, and also get maven to build cleanly? (Of course Eclipse can build just fine with just a project dependency.)
UPDATE
Using the Build Helper plugin to add the utility projects source folder to my pom was a viable path to the solution, but then I needed to update all the dependencies of the utility project into my new Mavne project, which started to make the whole process too time consuming (and also not really the chain of dependencies I wanted). I think that if I would have added all the dependencies, then Build Helper suggestion would have worked.
For now, I built the utility project jar and installed it into maven. Which turned out to be the the quickest solution. I will try to Mavenize the utility project, without modifying its structure (as suggested by FrVaBe below), and see if I can link the poms afterward.
I am going to keep this question open until I have a full solution which can be reported back, since I assume this is a problem others will have (trying to integrate legacy projects with new maven projects).
For the development time you can add the dependency as a System Dependency. It will be resolved by the file path (which can be the path to your utility.jar file under development) in this case.
It is added as describe in the link above, e.g.:
<dependencies>
<dependency>
<groupId>my-utility</groupId>
<artifactId>my-utility</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${somewhere}/lib/my-utility.jar</systemPath>
</dependency>
</dependencies>
The maven handling of System dependencies is sometimes special. E.g. they will not be included in war-packages! Therefore, when you are finished I would strongly recommend to install your utility library to the maven respository or to deploy it to a repository manager (Nexus/Artifactory).
You can add utility project's src folder to your working project in eclipse. For your development purpose.
right click on Working project
go to properties and choose java build path
go to source tab
Add your utility project src folder to that.
Later you can install your jar as maven dependency.

Adding a Maven project to Eclipse compiled project

I am trying to add java code from a Maven project (called docx4java) which I checked out from svn to an existing Eclipse project (called DocumentManager). I have tried the normal way, that I thought would work, i.e.:
Right Click on eclipse project>Properties>Java Build Paths> Projects > Add (here I add the Maven project) and >Libraries (here I specify Native Library location e.g. docx4/trunk/docx4/src/) but I still can't get the 'Maven' classes to be recognised in eclipse. I get the message
import docx4j.src.main.java.org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator cannot be resolved
I have tried adding a test project that was compiled in Eclipse and that works fine, Eclipse seems to recognise it i.e. import org.me.TestProject works fine.
How can I get the code from the Maven project docx4java to work in the Eclipse compiled project DocumentManager?
You can generate eclipse poject files (which you can just import into your Workspace) using the maven eclipse plugin
Take a look at these ecplise plugins for a more direct integration of maven into eclipse
You should create a eclipse project for docx4java as #Attila has commented. Once that is done, you should have two projects on your workspace: docx4java and DocumentManager. At this point, what you are doing now (adding a project reference to the Java Build Paths) should work.
It seems that the source path is not configured correctly, this is why the compiler cannot find your "Maven classes".
docx4java.src.main.java...
Maven has a different directory structure and you have to tell Eclipse where to find your sources.
For a "vanilla" eclipse project, remove the standard eclipse build path src and add the maven source path src/main/java (or whereever you added the code), the compiler should correctly pick up your sources.
Note that you still have to add all jars mentioned in the dependencies section of the maven pom.xml file. Otherwise it won't compile, even if it finds your added java classes.
(Addendum: I would recommend to use maven in your existing project as well.)

How do I get maven managed dependencies copied into war\web-inf\lib so I can run my GWT 2.0 app in debug mode within Eclipse?

I am updating an existing project from GWT 1.5.2 to GWT 2.0.0. We use maven 2 to manage our dependencies and do all of our development in Eclipse 3.5.
Because we use maven to manage our dependencies, I do not have all of those jars in the war\web-inf\lib directory as GWT specifies. Instead, they are in the maven repository, just where maven likes them. I have the project set up so that maven can successfully build and launch in either dev or web mode and the application runs correctly.
The problem is that when I launch from Eclipse, I get a java.lang.NoClassDefFoundError. If I manually copy of my dependencies into war\web-inf\lib before launching, everything runs fine, but that doesn't lend itself to a long-term solution. First, if I check all of those jars into our version control, that will subvert much of the value we get from maven. As annoying as maven can be, ditching it is not the answer. Second, having developers manually copy them over every time they want to debug something is ridiculous.
So can I get Eclipse to copy the dependencies into war\web-inf\lib before launching? Is there an alternate solution that I'm missing?
Thanks,
Tony
Running the gwt:eclipse goal will copy the maven dependencies into war/WEB-INF/lib. See the Eclipse Configuration section of the Eclipse IDE Integration documentation of the Maven GWT plugin for more details. Also have a look at this answer about Maven GWT 2.0 and Eclipse.
You should install the m2eclipse plugin and use that to build your project within eclipse. This will invoke maven as an external tool from within eclipse.
Your maven project artifact type should be set to war, which will let maven discover the dependencies and bundle them.
See these links:
force Maven2 to copy dependencies into target/lib
http://maven.apache.org/plugins/maven-war-plugin/
http://maven.apache.org/plugins/maven-war-plugin/examples/rapid-testing-jetty6-plugin.html
a maven aware IDE (idea, eclipse, netbeans) should do this packaging automatically. maybe you:
forgot to enable maven import inside IDE?
did not add these dependencies to the pom.xml (so they aren't included in the 'mvn package' phase)
added wrong scope to dependency declaration (e.g. scope 'provided' or 'tested'), so they are ignored for runtime
If you have the packaging method in your pom.xml set to war it should copy runtime depdencies into target/war/WEB-INF/lib.
Or is your project to build something larger like an ear? If so you should probably split your pom.xml into multi-project format.
As for Eclipse, I'm not terribly familiar with it so I can't really help you there. Are you (or can you) run a maven target (like "install") when you do a run or debug?
This is not supposed to be a problem. If you are using Eclipse+WTP then WTP lets you declare jars listed under 'J2EE module dependency' in the project's properties. Those jars are automatically published to tomcat (see note at the end of this comment)
You need to declare an M2_REPO variable in your eclipse environment (search eclipse help for 'classpath variables'), it should point to your local maven repository, and just add external jars from there to your project and mark them as 'J2EE dependencies'.
I did that with tons of projects, Maven and Eclipse live side-by-side :) (without m2eclipse)
A second option is to try to integrate Eclipse and Maven with m2eclipse plugin, as crowne mentioned, but I found this unnecessary.
There is a third option, if you don't use WTP (but you should). Assuming that you use tomcat, then you can copy those jars to $(tomcat.home)/common/lib. If you can do it just for your private instance of tomcat (on each developer's machine) then you are good to go.
This is supposed to be a low-impact solution that will get those jars into the classpath at runtime, but it doesn't scale well.
note: unfortunately the classpath of Eclipse web projects can be confusing. When you add jars to the regular build path of the project, they are not deployed (copied over) to tomcat. To solve this, WTP defines a special library called Webapp Library - this library contains all the jars in the project's WEB-INF/lib folder, as well as all the external jars that are marked as J2EE Module Dependencies. All those jars find their way appropriately to WEB-INF/lib when published to tomcat.
Quite simple:
1. Create a "lib" folder under your project like this:
"\src\main\webapp\WEB-INF\lib"
2. Copy needed "jars" etc that you want included inside your WAR bundle folder.
3. Invoke your maven build as you normally do. I use "mvn install", which creates builds the war file.
If you examine the WAR file, your jars that you included in step-1 and step-2 will be there.
Cheers.

Categories