Basic Maven (m2eclipse / pom.xml) configuration

Introducing Maven to Eclipse projects from scratch was described in my previous post. Now the basic usage and configuration will be described.

1a. Adding external jars (Maven dependencies)
To add some external jar file or reference to another workspace project you will need to:

Right Click on project -> Maven -> Add dependency

You can add it by filling in groupId, version etc. manually or search Maven repository by keyowrds. For example searching for JSON lib looks like in the picture below. After confirm, you will see changes reflected in pom.xml file.


1b. Another workspace project dependency
It is also possible to refer to another project, however it also has to be the Maven one (has its own pom.xml). Some instructions can be found here. Sample snippet of dependency in pom.xml is:

<dependency>
  <groupId>com.maventest</groupId>
  <artifactId>mytest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <scope>compile</scope>
</dependency>

2. Setting the src folder
The issue after adding Maven nature to project in my case was Eclipse missing the source folder in project. src was present in workspace, however it was not recognized as source folder. The solution is to add following lines in pom.xml file (if more info is needed look at this source):

 <build>
   <sourceDirectory>${basedir}/src</sourceDirectory>
 </build>

Note: Setting source folder by eclipse standard way (Build path -> Configure Build Path) will have no effect, because it will be reverted after Updating Maven Configuration.

3. Set JRE for project
If you want to use specific Java version in Maven project, you can use Maven plugin: compiler-plugin, as described here. It’s easy and requires only adding this entry to pom.xml file:

<plugins>
  ..
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
  </plugin>
</plugins>

4. Problem with selecting targets for Maven builds
If You have problem with selecting targets (empty goals list), see solution here and corresponding bug description here.

It did not help me :D However solution makes sense and worth to try it anyway :)

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!

2 thoughts on “Basic Maven (m2eclipse / pom.xml) configuration

Give Your feedback: