How to use Maven dependencies in eclipse auto import - java

I have a new maven project in eclipse and all maven stages work fine.
Problem comes when I write code.
I want classes in my dependencies to be available when I use organize imports.
On pressing Ctrl+Shift+O, eclipse does not use maven dependencies and I need to
manually type imports: this shows errors due to java builder
wait for maven build to compile
Can I not use eclipse java editor like I Used to with a simple java project which has some jars on classpath?
If so, how?
I hope there is a simple solution.
I don't want to spend more spend time mavenizing then writing code.

Use the Maven Eclipse plugin and tell it to download sources and javadocs for you or you can use M2eclipse(which automagically recognizes maven projects.)
use mvn eclipse:eclipse thanks to #Abdullah Shaikh
else
Window -> Preferences -> Java -> Code Style -> Organize Imports.(check
what your ctrl + shift +O do.?)

To convert a maven java project to support Eclipse, you can use below command to generate configuration files
mvn eclipse:eclipse
Run this command from the directory containing the pom.xml
In your case it will build classpath having all the dependencies mentioned in your pom
More details here maven eclipse plugin

mvn eclipse:eclipse
But don't forget to change the directory to your maven project first (command cd)
Then reboot Eclipse
Than go to Eclipse Explorer - right click on your file with import errors - Source -
organize imports (or Ctrl-Shift-O)

Related

Errors after building project using maven

I followed the following tutorial (https://github.com/kolorobot/spring-mvc-quickstart-archetype) and imported the project to eclipse.
However, i am getting the following errors in my pom.xml file, and also in all the java classes.
I have attached the screenshot.
POM.xml
Java classes
I have updated my Post
PROBEM VIEW
In the eclipse buildpath, make sure you have the maven dependency jars downloaded under "Maven Dependencies"
If you dont see, run the below command to download the same.
mvn clean dependency:copy-dependencies package
This may sound silly, but in my experience I have seen this working. Do a Project Clean in eclipse and do a maven build
There is no Maven dependencies classfolder in your screenshot. Have you tried right-clicking the project name -> Maven -> Update Project?
Update Unbound classpath var.M2_REPO
You need to fix your Maven installation. Go to Window -> Preferences -> Maven and have a look at: Installations and UserSettings

Make maven generate dependencies even with errors

This feels like a really stupid question but I haven't been able to find an answer.
I'm working on a maven project but I do most of my development in eclipse. Is there any way for me to force maven to generate all of my dependencies under target even if there are errors in the code? I set my eclipse project's build path to use the jars under target/dependencies/jars, but calling mvn clean kills them and if there are any errors in my code causing it to not compile mvn package won't create the dependencies but will instead just crash saying BUILD FAILURE. This makes the problem even worse since instead of seeing the actual errors my eclipse will just bombard me with errors everywhere since all of its dependencies just died.
Or maybe the way I'm working with it is just stupid and there's a better way.
Are you using the m2e plugins for Eclipse to process maven projects, or simply importing the projects as general ones?
If the latter, you should use the m2 plugins (simply go to the Eclipse Marketplace and search for Maven), as they interrogate your POM and set up your dependences properly. You can then concentrate on any compile errors in your code.
You should not point to the jars in the target folder for dependent JAR's since this is where the products of building your project are stored. Performing a mvn clean removes this folder.
To use Maven with Eclipse install the m2e plugin in Eclipse. This makes Eclipse understand the structure of Maven projects.
Once installed you can import your Maven project into Eclipse. I use Import... | Existing Maven Projects for this. But you can also directly import form a versioning system.
During the import Eclipse will set up the Eclipse project to use the Maven dependencies to locate the required JAR's. These are taken from the repository as configured with the used Maven installation.

How to use Maven with EAR

If i want to convert an EAR project a maven project , do i need to add the module in the deployment assembly as maven dependency or just use the convert in m2eclipse without any further configuration.
Me personally I wouldn't attempt any kind of conversion of an existing project. I would add the poms, make sure that mvn clean install works on the command prompt and then create a new mavenized Eclipse project from the poms.
The main reason is that you current project settings are effectively wrong when you switch to Maven - the Maven poms are the truth and what feeds the Eclipse project setup, so you really do not want to make your life difficult and work against m2eclipse - let it do the project creation for you. Fresh.
You can install m2eclipse and then do the following as well.
Go to the project menu (right click on Package Explorer) > Configure > Convert to Maven Project
Open the pom.xml and right-click and choose Run As -> Maven Clean. Similarly Choose Run As -> Maven Install.
Note : Please ensure that your eclipse project settings are correct and classpath libraries are not absolute and you don't have any project specific environment variables defined in your workspace. Please take a backup of your project before you do this.This is to ensure we don't mess up the current stable project configurations. Once m2eclipse generates the pom.xml for your project, you can update and make changes to it to
fully obtain a mavenized ear build. hope this helps
You can also try creating new maven project with archetype selection of "jboss-javaee6-ear" and follow the similar structure for your project. Most probably you will need parent Pom and child poms per each module (ejb, war, jar etc). There are other few similar approach but almost all of them requires you to have mulitple POMs
maven-ear-plugin and JBoss AS 7
You can also go through all the examples for maven ear plugin to find settings suitable for you
http://maven.apache.org/plugins/maven-ear-plugin/
I ended up ditching ear for war :) single POM and even ditched the JBOss for tomcat/jetty :)
If you want to convert your existing eclipse dependencies into Maven dependencies, you can try the JBoss Tools (JBT) Maven integration feature, which contains an experimental conversion wizard, plugged into m2e's conversion process : http://docs.jboss.org/tools/whatsnew/maven/maven-news-4.0.0.Beta1.html.
So, all you have to do is, as Keerthi explained, right-click on your project and Configure > Convert to Maven...
If your dependencies already are maven artifacts, it should work easily. If not, you'll need to convert them to Maven (if they're workspace projects) or make them available in your maven enterprise repository, before converting the EAR project.
JBT (requires Eclipse JavaEE) can be installed from http://download.jboss.org/jbosstools/updates/stable/kepler/ or from the Eclipse Marketplace (See https://marketplace.eclipse.org/search/site/jboss%2520tools)

Trying to compile Maven with clean parameter and got exception

Trying to compile Maven with clean parameter:
C:> mvn clean
and got the exception below:
Cannot execute mojo: clean. it requires a project with an existing pom.xml, but the buid is not using one.
can anyone tell me how I can associate my existing project to maven.
Maven uses a file called pom.xml to build. It should be located in the root of your project.
Maven works on the basis that your project conforms to the Maven way of doing things and so I would recommend reading the Maven Getting Started Guide to familiarize with the standards before starting to use Maven.
I think you need to read up on a bit of documentation. Basically it is the pom.xml file it is complaining about that defines how maven should handle your project.
This explains how you can use maven to generate an example project.
This goes in more depth and also provides some links to other resources.
suppose your project is in 'C:\MyProject' where you can see a pom.xml file, open command prompt, go to C:\MyProject and type 'mvn clean' as follows:
> cd C:\MyProject
> mvn clean
The project you are compiling is not a maven project.

Eclipse m2eclipse clean, build projects, build automatically commands, what are they?

What is the command that m2eclipse runs when you
highlight a project -> Menu Project -> Clean -> Select anything -> Ok ?
It then goes on to "Building Workspace".
What is the command that allows it to do so?
The reason I am asking is that I am trying to do this from outside Eclipse, from the command line. I am trying to automate all maven stuff in a Groovy script. I am on Windows xp.
EDIT:
Also, the command update maven dependencies would be nice to have as well.
Thanks!
Maven-invocations are put in the Run and Debug menus. Just building the workspace does not run maven - it just does all the work Eclipse needs to do to know your files.
For your purposes these will suffice for the command line build:
cd /to/where/pom.xml/is
mvn clean
mvn install
No commands are directly run. Instead, the m2e plugin uses the configuration in the pom to create equivalent configurations in eclipse.
For example, if you change the source directories it will update the eclipse source directories for the java builder. If you change the target directories for the source compilation, it'll ensure that when you do a Project -> Clean, the plugin knows which directories to clean.
If you are trying to do this though a scripting language, I would recommend just running commands on the command line. The alternative would be to add the maven libraries to the classpath and to use them.

Categories

Resources