maven + elicpse related questions - maven

Forgive me asking following questions. I am totally lost in regards to maven+eclipse. I checked out someone's java project (maven built) from SVN to my local eclipse (kepler). When I click Windows > Preferences, I see Maven.
question 1)
Is this a maven plugin? When developers say maven in eclipse, are they referring to maven plugin? maven and maven plugin are two separate components?
question 2)
when I click on user settings, C:\Users\myName.m2\settings.xml is missing. Exact error message is "User settings file doesn't exist". Does it get created when you install maven plugin at first time?
question 3)
I found three folders may have to do with maven C:\workspace\maven_local_repo_artifactory directory, C:\maven_local_repo and C:\Users\myName.m2\respository but not sure how they get created and what is the relationship among them.
question 4)
Is it ok to remove current maven plugin from eclipse and re-install it then check out the java project from SVN? I think my maven or maven plugin settings are not correct in my local box.

1) Is this a maven plugin? When developers say maven in eclipse, are
they referring to maven plugin? maven and maven plugin are two
separate components?
Yes. This is the maven-plugin. maven-plugin uses the configurations of maven (%M2_HOME%/conf).
If you wanna work with maven, you need to install it on your machine. Then you can run maven commmands. In addition, if you want to invoke maven commands within eclipse (conveniently) - you can install the eclipse-plugin. "maven-plugin" is a plugin for eclipse, that lets you use maven within Eclipse conveniently.
2) when I click on user settings, C:\Users\myName.m2\settings.xml is
missing. Exact error message is "User settings file doesn't exist".
Does it get created when you install maven plugin at first time?
By default, the maven-plugin assumes that your settings.xml (which is the configuration file of maven) is in the path you have mentioned. However, there are cases (like in my case) where the config file is not there, but under %M2_HOME%/conf. you can update it in Eclipse, and the error will disappear.
3) I found three folders may have to do with maven
C:\workspace\maven_local_repo_artifactory directory,
C:\maven_local_repo and C:\Users\myName.m2\respository but not sure
how they get created and what is the relationship among them.
C:\Users\myName.m2\respository is the "local repository". If you learned a bit about how maven works, it holds a local repo on the local machine, and it keeps there all artifacts. It downloads them from the "repository" - if you have one in your company (Nexus, Artifactory, etc) or from Maven Central. However, this path is configurable by Maven's settings. So there might be that someone played with it and changed the path, and these other directories were created. You did not mention what resides inside these paths...
4) Is it ok to remove current maven plugin from eclipse and re-install
it then check out the java project from SVN? I think my maven or maven
plugin settings are not correct in my local box.
Sure it is OK. You may remove the plugin, and the source plus maven itself will not be deleted from your machine.
HTH.

Related

Do we have to include jar files in eclipse project if we are using maven?

I am new to maven.
The POM file in maven contains all the dependencies that we need in our project.
So we don't have to externally add any JAR's to the buildpath in eclipse.Right?
That's right. Maven will download dependencies and M2Eclipse (Eclipse plugin for integrating Eclipse with Maven) will setup a build path for you.
Two Solutions - 1 Using Eclipse IDE
Install the Maven (M2E Eclipse Plugin) if you use older version of eclipse, If you download the latest eclipse.
Point your settings.xml and create a maven project from your eclipse, it is better keep Group Id as com.yourcompany.app Artifact Id as yourProjectName and Version 0.0.1-SNAPSHOT depends on your Architecture Standards set by your company. Also Packaging can be as WAR file for WebApplication, EAR file for Enterprise Application. You can find the numerous list of examples from Maven Site.
2 From Command Line
Install Maven from Apache Maven site, Would recommend to go for the latest version -apache-maven-3.3.9-bin.zip.
Set the Maven Home in the Environment Variables as shown in the below figure.
Edit Your Path variable as %MAVEN_HOME%\bin, verify your installation by using this command from your command prompt. It should display the Maven Home and Java Version, which confirms your maven successful installation.
Paste your settings.xml (C:\apache-maven-3.3.9\conf) given by your build team or Architecture team for accessing your internal repository. And keep a backup of the original settings.xml (which is default download from Maven site)
Run these commands from your command prompt.
mvn eclipse:clean -e
mvn eclipse:eclipse -e (which will automatically set your project build path as shown in the below figure)
It will resolve your compilation issues and your project is ready as an deploy-able artifact

IntelliJ IDEA does not create artifacts from pom.xml

I use the IntelliJ IDEA 12.1.4 under Windows and Linux (Ubuntu). The Maven plugin is installed.
I have a big maven project with many poms. Under Windows the IDEA creates artifacts automatically when I do reimport from maven. But under my Linux system the list of artifacts is empty always.
Is it a bug? How to generate artifacts in the Linux case?
Go to the Maven-Projects-View and click "package" for your aggregator project. IntelliJ will then run Maven which creates all artifacts in the target folder. If you run "install", these artifacts will be copied to the local repository. Maybe you didn't do that under Linux yet, so your repository is empty - under Windows, I suppose that IntelliJ was smart enough to refer to those artifacts while importing the project. I personally do not know of any feature in IntelliJ that would automatically package/install all projects on reimport - I'd consider this rather annyoing.

IntelliJ Maven is correctly generating maven local repository but not adding the dependencies

Hi I am trying to port a mid sized Maven project to IntelliJ Idea 12 (from Eclipse).
There are around 30 different modules in the project.
I am running an MVN install on each module via IntelliJ lifecycle management.
The jars are being correctly generated, and deposited into my local repository directory. It is also correctly picking up the third party libraries.
However IntelliJ is sometimes requiring me to then add the generated jars to my classpath as a dependency. (It is not enough to simply say "Add Maven Dependency", I have to physically add the generated jar as a library.)
In other cases it works correctly. Not sure why it is not consistent.
Have you tried updating the local Maven repository in IntelliJ IDEA? You can do so by opening Preferences->Maven->Repositories, than select your local repository and click on 'Update'.

Trouble with maven in Netbeans

I want to create maven project in Netbeans. So, I do File->New project->Maven->Java Application. After that I try to build the project and get error:
The POM for org.apache.maven.plugins:maven-surefire-plugin:jar:2.10 is missing, no dependency information available.
But I can build this project from command line with mvn compile. Could uou tell me what is the problem with Netbeans?
NetBeans is using 3.0.4 maven by default. Unless you change that in Tools/Options menu. Are you building with 3.0.4 as well or are you using some earlier versions (2.x)?
That would explain the behaviour because 3.0.4 will not blindly rely on what artifact is in local repository but some additional metadata is also consulted to make sure your project build with the given set of defined repositories.
A common example when the problem occurs to me.
I use central directly everything downloads. when I later add a mirror, all artifacts are checked again through the mirror to make sure they are accessible. if teh Mirror doesn't actually mirror central, I get an error that way.
Another common example is: when building with 2.x, the additional metadata is not written, when later building with 3.0.4, all remote context is checked no matter what is present in local repo and the additional metadata files are constructed.

Maven can't discover workspace projects, junit, other libraries

so I have started using maven (with eclipse, and a local implementation of Sonatype) not that long ago, after getting through the initial pain I though everything was working.... apparently not !
I have my main project POM and all my child POM (I am using a 'flat' hierarchy, so all the sub projects are in the same folder as the parent POM). This seems to be working OK.
The sub project where all started and 'released' and tested before I started using Maven. I went to maven to solve my 'dependencies' problems... or rather make them more complicated in this instance.
Most of the 'Maven' stuff seems to work fine, but I can't resolve the dependencies of my 'sub projects' that have been released into my local ms/repository
Each day I start up eclipse and have the same problems, I have 'unable to resolve imports' on all my personal libraries, and all those that I use for them (such as Log4j, DDlUtils etc).
I thought the whole point of maven was that I would be able to put in an import declaration for a library, add the 'groupID' and other stuff to the child POM (or parent POM). And then have all the jars downloaded automagically when I run
mvn install
against the parent.
However each day I find I have to re-insert the build path to the libraries (internal and external) via the eclipse workspace and point them to the location of the libraries that I have downloaded as maven can't seem to find them anywhere.
Specifically with running Junit test, I can run them from the workspace (using run as junit) and they all pass fine. But if I try to run them with
mvn test
I get an message saying
java.lang;NoClassDefFoundError: org/junit/Assert
(and this is the same from within eclipse running the mvn test or from CLI)
with an error message in the output of
failed to execute goal org.apache.maven.plugins:maven.surfire-plugin:2.10:test
the POM and jar are in my M2_REPO, and I can find it when I search in sonatype, why can't maven find something that is part of its own system, exists in its repo.
but this obviously works just fine when I run via eclipse and point it to an independently downloaded junit library.
I don't know what extra info you may require, and I'm obviously missing something in either the m2e plugin, my maven install or in sonatype.
please help I'm begining to loose my hair!
and may soon be forced to return to an ant/ivy solution (but don't want to have to learn 'yet another tool' (in this case ivy).
Thanks in advance
David.
ps. i'm on a windows XP platform
edit...
can I get maven to build and release the jar and pom even though it thinks it is failing the tests (which is itn't as I'm doing them manually, and they pass just fine).
If I look in the 'effective POM' tab of my parent POM, the plugin stated above is not in there. I guess this means it is a maven config setting, but where do I add it, and why isn't it automatic when I run my first set of maven tests?
edit 2....
I just found this on the apache plugin comments page (http://maven.apache.org/plugins/maven-surefire-plugin/plugin-info.html), so I ran the code on the CLI, mvn surefire:help -Ddetail=true -Dgoal=test lots of stuff was downloaded, and now the tests run, but they fail within maven ? (remember they pass when run from eclipse using 'run as junit test)... so this is nearly a solution...
It sounds like you need make eclipse aware that the projects in question are Maven projects. You may have POMs set up for your projects, but for the m2e plugin to work you need to have either created the project as a Maven project ('New' -> 'Project...' -> 'Maven' -> 'Maven Project...'), or converted it into a Maven project by right clicking it then selecting 'Configure' -> 'Convert to Maven Project'. You will know that a project has been recognised as a Maven project because there will be a little blue 'M' in the top left of its icon, as shown in the screenshot below:
The main thing to note in the above picture is the 'Maven Dependencies' library. This is set up automatically by the m2e plugin. Whenever you add a dependency to a POM then the project's build path will be configured automatically, although sometimes you may need to force it to do so by right clicking the project and selecting 'Maven' -> 'Update Maven Configuration'. The important thing is that if everything is working then you should never have to update the build path yourself. Moveover, if you do update it yourself then any changes you make will likely get overwritten the next time you run the 'Update Maven Configuration' command.
Also worth noting from the picture is that the two dependencies 'mavenProjectTest' and 'primes' have folder icons next to them. This means that they have been picked up as workspace projects. For this to work the project must have 'workspace resolution' enabled, and the projects to be picked up need to be configured as m2e Maven projects as well.
You mention downloading external libraries. You shouldn't need to download any libraries yourself - by adding the right dependency declaration for an external library then Maven will download it from your configured remote repository (the first time anyway - afterwards it will then be able to get it from your local repository). By default, this is the Maven Central repository. To add an external library to your project just follow that link, enter the library in the search box, click on the version link for the version you require, then you will be taken to a page where there will be the dependency XML declaration that you can just copy and paste into your POM.
One more thing that may help is that you should make sure that your source folders follow the Maven default directory structure. That is, your test packages should be contained in a source folder called 'src/test/java', and the main project packages should go in 'src/main/java'. Otherwise Maven will not know where to find your source code. It is possible to configure a POM to tell Maven to expect your source code to be in different source folders, but it is highly recommended that you follow the standard Maven directory structure.
Since you have child modules, my suggestion would be to simplify things by creating a new Maven project from scratch that you can have a play around with. Once you are comfortable with that then try getting m2e to work for your multi-module project.
I remember your pain when first getting to grips with Maven, but it's great once you understand what it's doing and everything is working. I highly recommend reading through the free online book Maven: The Complete Reference - it helped me out a lot when I was getting started with Maven.
I have experienced the same messages and have found a working solution, thanks to one of my organization's Maven experts.
Here was my pom.xml that reproduces your error:
<dependencies>
<dependency>
<groupId>org.junit4</groupId>
<artifactId>org.junit4</artifactId>
<version>4.3.1</version>
</dependency>
</dependencies>
With it, I would get package org.junit does not exist messages and NoClassDefFoundError: org/junit/Assert. Similar to your experience, it worked great from Eclipse, green bars and all.
Here is the pom.xml that works:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
I copied this from the example at the top of this Using JUnit page.

Resources