Maven


elipse project to maven
right click on project > Configure > Convert to Maven Project
$ mvn clean install
$ mvn compile
$ mvn clean package
To run:
$ mvn exec:java -Dexec.mainClass="lidar.ruleBased.GenerateElevationPercentileRulePacks"
OR
$ java -cp target/SarahLeilaPostProcessor16-0.0.1-SNAPSHOT.jar lidar.ruleBased.GenerateElevationPercentileRulePacks








From eclipse and m2e
Eclipse>File>New>Project>Maven Project
Right click on project > Run as Maven Install
Change java code to 1.8
<build>
  <plugins>
	<plugin>
		<artifactId>maven-compiler-plugin</artifactId>
		<version>2.3.2</version>
		<configuration>
			<source>1.6</source>
			<target>1.6</target>
		</configuration>
	</plugin>
   </plugins>
  </build>


remote debugging http://docs.alfresco.com/4.2/tasks/dev-extensions-maven-sdk-eclipse-remote-debugging.html
to run a class            mvn exec:java -Dexec.mainClass="com.example.Main"



Creating a Project

Go to a directory you want your project to reside.
mvn archetype:generate -DgroupId=edu.ufl.cise -DartifactId=corpusBatchProcessor -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
$ cd corpusBatchProcessor
corpusBatchProcessor
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- edu
    |           `-- ufl
    |               `-- cise
    |                   `-- App.java
    `-- test
        `-- java
            `-- edu
                `-- ufl
                    `-- cise
                        `-- AppTest.java

This project's POM created is:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>edu.ufl.cise</groupId>
  <artifactId>corpusBatchProcessor</artifactId>
<version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>Maven Quick Start Archetype</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> </dependencies> </project>
Let it know you want to generate a runnable jar file. just copy the bold section below in your pom.xml file. just declare your main classes below. The META_INF/MANIFEST.MF  file will be generated.
<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>edu.ufl.cise</groupId>
  <artifactId>corpusBatchProcessor</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>corpusBatchProcessor</name>
  <url>http://maven.apache.org</url>
  <build>
        <plugins>
            <plugin> 
                <artifactId>maven-jar-plugin</artifactId> 
                <configuration> 
                    <archive> 
                        <manifest> 
                            <mainClass>edu.ufl.cise.CorpusBatchProcessor</mainClass> 
                        </manifest> 
                    </archive> 
                </configuration> 
            </plugin> 
            <plugin> 
                <artifactId>maven-assembly-plugin</artifactId> 
                <configuration> 
                    <descriptorRefs> 
                        <descriptorRef>jar-with-dependencies</descriptorRef> 
                    </descriptorRefs> 
       <archive> 
                        <manifest> 
                             <mainClass>edu.ufl.cise.CorpusBatchProcessor</mainClass> 
                        </manifest> 
                    </archive>
                </configuration> 
                <executions> 
                    <execution> 
                        <id>make-my-jar-with-dependencies</id> 
                        <phase>package</phase> 
                        <goals> 
                            <goal>single</goal> 
                        </goals> 
                    </execution> 
                </executions> 
            </plugin> 
        </plugins> 
    </build> 
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
  </dependency>
</project>

So far you can call $ mvn clean install and $mvn compile to see the jar file being generated

add the dependency to it. you can get them the details from http://mvnrepository.com/ you can look there to find more dependencies that your libraries might have 
OR store your jar files in a local directory 
 if you want to, as mentioned a couple of pages below
<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>edu.ufl.cise</groupId>
  <artifactId>corpusBatchProcessor</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>corpusBatchProcessor</name>
  <url>http://maven.apache.org</url>
  <build>
        <plugins>
            <plugin> 
                <artifactId>maven-jar-plugin</artifactId> 
                <configuration> 
                    <archive> 
                        <manifest> 
                            <mainClass>edu.ufl.cise.CorpusBatchProcessor</mainClass> 
                        </manifest> 
                    </archive> 
                </configuration> 
            </plugin> 
            <plugin> 
                <artifactId>maven-assembly-plugin</artifactId> 
                <configuration> 
                    <descriptorRefs> 
                        <descriptorRef>jar-with-dependencies</descriptorRef> 
                    </descriptorRefs> 
       <archive> 
                        <manifest> 
                             <mainClass>edu.ufl.cise.CorpusBatchProcessor</mainClass> 
                        </manifest> 
                    </archive>
                </configuration> 
                <executions> 
                    <execution> 
                        <id>make-my-jar-with-dependencies</id> 
                        <phase>package</phase> 
                        <goals> 
                            <goal>single</goal> 
                        </goals> 
                    </execution> 
                </executions> 
            </plugin> 
        </plugins> 
    </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.apache.thrift</groupId>
    <artifactId>libthrift</artifactId>
    <version>0.9.0</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>            
  </dependencies>
</project>

again run 
$ mvn clean install
$ mvn compile
$ java -jar target/java -jar target/corpusBatchProcessor-1.0-SNAPSHOT-jar-with-dependencies.jar          # and run your jar file!!!

mvn eclipse:eclipse          # if you want to import your project to eclipse.



Install Maven:

Download and unzip maven

export M2_HOME="/home/morteza/zproject/packages/apache-maven-3.0.5"
export M2=$M2_HOME/bin
export PATH=$PATH:$M2
export MAVEN_OPTS="-Xms256m -Xmx512m"     


Local Maven Repository

Local Maven Repository is used to store all the projects’ dependency libraries. When you build a project with Maven, it will automatically download all dependency libraries into Maven local repository. It is located at ~/.m2 on Linux and C:\Documents and Settings\username\.m2 on Windows
It is good practice to set the maven local repository to somewhere besides $M2_HOME itself.
To change it add <localRepository>C:/java/.m2/repository</localRepository> to {M2_HOME}\conf\setting.xml




A Simple Java-Maven Project

Create

C:\tutorials>mvn archetype:create -DgroupId=com.mytutorial -DartifactId=simple
The structure of the project will be :
simple\
simple\pom.xml

simple\src\main\java\com\myTutorial\App.java (created by default)
simple\src\test\java\com\myTutorial\AppTest.java

Eclipsify!

C:\tutorials\simple>mvn eclipse:eclipse   //Generate Eclipse project meta-data to this maven project:  ~A better way is to import via m2eclipse plugin

Import to Eclipse

Now import the project to eclipse (simplest eclipse for java developers).
Add M2_REPO classpath variable in Eclipse : Window -> Preferences in the tree that is on the left side of the windows Java -> Build Path -> Classpath Variables set it to $M2_HOME\repository

Run App.java

These directories will be created:
simple\target\classes\com\mytutorial\App.class
simple\target\test-classes\com\mytutorial\AppTest.class
Run AppTest.java as JUnit Test.

[  Compile

$ mvn clean install   ]   mvn -U clean install   to update repositories and download again

Make Jar

C:\tutorials\simple>mvn clean package  //It makes a Jar of the project at simple\target\simple-1.0-SNAPSHOT.jar

Run Jar

java -cp simple-1.0-SNAPSHOT.jar com.mytutorial.App
-cp stands for classpath. I.e. where to find your class or classes.
com.mytutorial.App Fully qualified class name.

Share it

C:\tutorials\simple>mvn install    // it will copy the jar file of the project to the $M2_HOME\repository\com\mytutorial\simple\1.0-SNAPSHOT
com.tutorial -> is the groupid
simple -> is the artifact
1.0-SNAPSHOT -> is the package version
--------------------------

To Modify Project Dependencies

Add any additional dependencies into your “pom.xml” file and then run “mvn eclipse:clean eclipse:eclipse” command to get all your eclipse build path dependencies set automatically.

Java Web Maven Project

Create a skeleton Maven based Web project structure (remember: maven is all about“convention over configuration” i.e. consistency) using the following archetype plug-in command. Remember that heart of maven is plug-ins and you can look at the syntax for different plug-ins at http://maven.apache.org/plugins/index.html.


Create

C:\tutorials>mvn archetype:create -DgroupId=com.mytutorial -DartifactId=simpleWeb -DarchetypeArtifactId=maven-archetype-webapp
The structure of the project will be :
simpleWeb\
simpleWeb\pom.xml
simpleWeb\src\main\resources\
simpleWeb\src\main\webapp\index.jsp   \\webapp ieven in apache is where the web documents are located, hence index.jsp would be home
simpleWeb\src\main\webapp\WEB-INF\web.xml     \\  WEB-INF\web.xml is the mandatory structure of all web projects
Add the following lines to pom.xml inside build tag after fileName tag:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.4</version>
<configuration>
<downloadSources>false</downloadSources>
<wtpversion>1.5</wtpversion>
</configuration>
</plugin>
</plugins>
</pluginManagement>

Eclipsify

Eclipse JEE version.
C:\tutorials\simpleWeb>mvn eclipse:eclipse
It looks like by It looks likeby merely calling this, the project will not be a web-enabled project in eclipse (WTP). To make the project runnable via eclipse servers portion,

Configure Apache for Eclipse: Download and unzip Apache Tomcat somewhere. In Eclipse Windows -> Preferences -> Servers -> Runtime Environments add (Create local server), select your version of Tomcat, Next, Browse to the directory of the Tomcat you unzipped, click Finish.

Window -> Show View -> Servers    Add the project to the server list


Eclipse Plugin

To use maven on Eclipse :

For Maven, install m2plugin, it would give warning that it doesn’t recognize JDK. Add

–vm

C:\Program Files\Java\jdk1.6.0\bin\javaw.exe

To the eclipse.ini. right before any -vm configurations.  It would be OK

  1. Select Help -> Software Updates
  2. Select Available Software, click Next
  3. Click Add Site
  4. Enter http://m2eclipse.sonatype.org/sites/m2e for the URL
Just goto:   File->Import>Maven->Existing Maven Projects


General Notes

The plug-ins that are referred to in the pom.xml are only downloaded if they are needed.




Maven -DarchetypeArtifactId

Project Template (Archetype) Purpose
maven-archetype-archetype Create your own project template (archetype).
maven-archetype-j2ee-simple Creates a J2EE project (EAR), with directories and subprojects for the EJBs, servlets, etc.
maven-archetype-mojo Create your own Maven 2 plugins.
maven-archetype-quickstart Simple Java project, suitable for JAR generation. Maven 2 default.
maven-archetype-site Documentation-only site, with examples in several formats. You can run this archetype on top of an existing Maven 2 project to add integrated documentation.
maven-archetype-webapp Creates a web application project (WAR), with a simple Hello World JSP.

Maven Commands

mvn clean Cleans out all Maven-2-generated files.
mvn compile Compiles Java sources.
mvn test-compile Compiles JUnit test classes.
mvn test Runs all JUnit tests in the project.
mvn package Builds the JAR or WAR file for the project.
mvn install Installs the JAR or WAR file in the local Maven repository (use this if you have multiple interdependent local projects).


some popular plugin commands

clean:clean Cleans up after the build.
compiler:compile Compiles Java sources.
surefire:test Runs the JUnit tests in an isolated classloader.
jar:jar Creates a JAR file.
eclipse:eclipse Generates an Eclipse project file from the pom.xml file.


To add a dependency, to find the groupid:artifactid:version of the resource you are looking for, go to:
http://repo1.maven.org
e.g. if you want to add jsf to your project, you can find it at http://repo1.maven.org/maven2/javax/faces/jsf-api/1.1_02/jsf-api-1.1_02.pom
If a particular dependency jar in your pom.xml file is not found in the local repository c:\java\.m2\repository, maven 2 will download this from its repository at http://repo1.maven.org/maven2

According to Maven convention all the java files should go under src/main/java and all the resources like .properties files should go under src/main/resources folder. When they are packaged (i.e. war file), both the “PersonBean.class” & “messages.properties” end up under “WEBINF\classes\com\mytutorial” folder as shown below.







Comments