Wednesday, December 3, 2014

Create a Java Project using Maven

Environment


The versions in this tutorial shouldn't matter too much, but I'm using:
  1. Eclipse Java EE IDE for Web Developers.
    Version: Luna Service Release 1 (4.4.1)
    Build id: 20140925-1800
  2. Apache Maven 3.2.3
  3. Oracle JDK 1.7.0_71


Outline

  1. Creating the Project
  2. Cleaning the Project
  3. Importing the Project


Create the Project


 mvn archetype:generate   
      -DgroupId=<my-group-id>  
      -DartifactId=<my-artifact-id>  
      -DarchetypeArtifactId=maven-archetype-quickstart   
      -DinteractiveMode=false   

The groupId is unique among all Java projects.  This generally conforms to the Java packaging naming standards.  The artifactId is used in artifact generation and For example, a project might have a dependency on lucene-core (artifactId) and the source code in this project has the package structure "org.apache.lucene.core".


Operational Output

 $ mvn archetype:generate -DgroupId=org.swtk.common.qa -DartifactId=commons-qa -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false  
 [INFO] Scanning for projects...  
 [INFO]  
 [INFO] ------------------------------------------------------------------------  
 [INFO] Building Maven Stub Project (No POM) 1  
 [INFO] ------------------------------------------------------------------------  
 [INFO]  
 [INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) > generate-sources @ standalone-pom >>>  
 [INFO]  
 [INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) < generate-sources @ standalone-pom <<<  
 [INFO]  
 [INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---  
 [INFO] Generating project in Batch mode  
 [INFO] ----------------------------------------------------------------------------  
 [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0  
 [INFO] ----------------------------------------------------------------------------  
 [INFO] Parameter: basedir, Value: C:\projects\swtk-common  
 [INFO] Parameter: package, Value: org.swtk.common.qa  
 [INFO] Parameter: groupId, Value: org.swtk.common.qa  
 [INFO] Parameter: artifactId, Value: commons-qa  
 [INFO] Parameter: packageName, Value: org.swtk.common.qa  
 [INFO] Parameter: version, Value: 1.0-SNAPSHOT  
 [INFO] project created from Old (1.x) Archetype in dir: C:\projects\swtk-common\commons-qa  
 [INFO] ------------------------------------------------------------------------  
 [INFO] BUILD SUCCESS  
 [INFO] ------------------------------------------------------------------------  
 [INFO] Total time: 3.802 s  
 [INFO] Finished at: 2014-12-12T15:58:12-08:00  
 [INFO] Final Memory: 15M/491M  
 [INFO] ------------------------------------------------------------------------  

Using WinDirStat, let's look at what Maven has visualized for us:

The generated directories conform to Maven's standard directory layout.  There is a already a simple Java class and even simpler test harness in place as a placeholder to run the Maven build lifecycle.



Clean the Project


Run the package command, as part of the maven build lifecycle to validate the new project:
 mvn clean package  



Import into Eclipse


As it stands right now, we will not be able to import this project into Eclipse, until we instruct Maven to add an Eclipse nature to the project.

Run this command:
 mvn eclipse:eclipse  

Operational Output

 $ mvn eclipse:eclipse  
 [INFO] Scanning for projects...  
 [INFO]  
 [INFO] ------------------------------------------------------------------------  
 [INFO] Building commons-qa 1.0-SNAPSHOT  
 [INFO] ------------------------------------------------------------------------  
 [INFO]  
 [INFO] >>> maven-eclipse-plugin:2.9:eclipse (default-cli) > generate-resources @ commons-qa >>>  
 [INFO]  
 [INFO] <<< maven-eclipse-plugin:2.9:eclipse (default-cli) < generate-resources @ commons-qa <<<  
 [INFO]  
 [INFO] --- maven-eclipse-plugin:2.9:eclipse (default-cli) @ commons-qa ---  
 [INFO] Using Eclipse Workspace: C:\Backup\Java\workspaces\swtk\common  
 [WARNING] Workspace defines a VM that does not contain a valid jre/lib/rt.jar: C:\Program Files\Java\jre7  
 [INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER  
 [INFO] Not writing settings - defaults suffice  
 [INFO] Wrote Eclipse project for "commons-qa" to C:\Backup\Java\workspaces\swtk\common\projects\swtk-common\commons-qa.  
 [INFO]  
 [INFO] ------------------------------------------------------------------------  
 [INFO] BUILD SUCCESS  
 [INFO] ------------------------------------------------------------------------  
 [INFO] Total time: 0.797 s  
 [INFO] Finished at: 2014-12-12T16:07:38-08:00  
 [INFO] Final Memory: 11M/491M  
 [INFO] ------------------------------------------------------------------------  


Then import the project into your Eclipse workspace:
File > Import > Existing Projects into Workspace


I find that's it's necessary with this current version of Eclipse and Maven to perform an additional step at this point:
Right-Click on the Project > Configure > Convert to Maven Project
Eclipse will likely update the project with some default values, including JDK 1.5.

It will be necessary to explicitly set the JDK build level in the POM file like this:
 <build>  
      <plugins>  
           <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.2</version>  
                <configuration>  
                     <source>1.7</source>  
                     <target>1.7</target>  
                </configuration>  
           </plugin>  
      </plugins>  
 </build>  

By using these values in the POM file, Eclipse and Maven will stay in sync with Java compiler levels.


Automation


Under Windows, I like to add this batch script to my bin directory of Maven (or anywhere on the path):
 @echo OFF  
 REM http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true  

 mvn archetype:generate -DgroupId=%~1 -DartifactId=%~2 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false  
cd %~1
mvn eclipse:eclipse
mvn build
cd ..

On either OS X or Linux/Ubuntu, this shell script works great:
mvn archetype:generate \
 -DgroupId=$1 \
 -DartifactId=$2 \
 -DarchetypeArtifactId=maven-archetype-quickstart \
 -DinteractiveMode=false
pushd $1
mvn eclipse:eclipse
mvn build
popd

Then, when creating a new Java project, I can simply invoke:
mvn-gen com.mycompany.project my-project
Remove the mvn eclipse:eclipse command if this is not your IDE.


References

  1. Introduction to Archetypes
    1. Archetype is a Maven project templating toolkit
    2. An archetype is defined as an original pattern or model from which all other things of the same kind are made. The names fits as we are trying to provide a system that provides a consistent means of generating Maven projects. Archetype will help authors create Maven project templates for users, and provides users with the means to generate parameterized versions of those project templates.
  2. Standard Directory Layout
    1. Having a common directory layout allows users familiar with one Maven project to immediately feel at home in another Maven project. The advantages are analogous to adopting a site-wide look-and-feel.
  3. Creating a Web Project with Maven (and without Eclipse)

25 comments:

  1. Good Blog post on Maven, all the code examples and screenshot images are good. Thanks for writing useful post.

    Best Regards,
    Best DevOps Online Training Institute in India

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai.

    ReplyDelete
  3. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.
    rpa training in bangalore
    rpa training in chennai
    rpa training in pune
    best rpa training in bangalore

    ReplyDelete
  4. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  5. Thank you for sharing this informative post.MyAssignmenthelp.com is giving assignment help to students.we are already trusted by thousands of students who struggle to write their academic papers and also by those students who simply want
    research paper writing service
    to save their time and make life easy.

    ReplyDelete
  6. Attend The Artificial Intelligence Online courses From ExcelR. Practical Artificial Intelligence Online courses Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Artificial Intelligence Online courses.
    ExcelR Artificial Intelligence Online courses

    ReplyDelete
  7. Great Information sharing .. I am very happy to read this article .. thanks for giving us go through info.Fantastic nice. I appreciate this post.
    Research Paper Writing

    ReplyDelete
  8. A Dissertation Help is written within the kind of a long essay that is usually required for getting the degree at the doctoral level. It needs a thorough examination of the chosen topic. the aim that a dissertation writing serves is producing a valuable piece of writing that is also the amalgamation of a subject that's considerable with apt research using the understanding and knowledge that the author has acquired through his academic years.

    ReplyDelete
  9. Thanks for sharing content and such nice information for me. I hope you will share some more content about. Please keep sharing!
    Artificial Intelligence Training In Hyderabad

    ReplyDelete
  10. Thank you for sharing such an informative article. Most people buy coffee already roasted, but if you want the freshest, most flavorful beans, you may want to roast your own. Before roasting, coffee beans are green and grassy. The high temperatures used to roast the beans cause chemical changes to bring out the aroma and flavor that you love. Visit Ikawa Home Coffee Roaster for more.

    ReplyDelete
  11. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.
    data science training in lucknow

    ReplyDelete
  12. Continue writing great articles like this. I have bookmarked your site and even shared to my friends. Notably, majority of academic institutions require student to write a capstone project as part of their graduation requirements. It is arguably true that writing a capstone project is one of the most challenging academic tasks that you will ever do as a student. This is because when working on a capstone paper, students are expected to synthesize the knowledge learnt over the period in which they have studied a given course in order to solve a real-world problem. Learn more from BSN Capstone Writing Help .

    ReplyDelete
  13. I love the art of writing you possess. Good job man. Writing capstone project is paved with myriad challenges. At this point, there’s one important but unanswered question: “who’ll do my capstone project?” That’s a great question to pose at this time. But it may not be the best question someone in your predicament should ask. The best question to ask would be: “i need help with my capstone project?” That’s because a successful capstone project is rarely the result of unassisted effort. So, stop digging around the web for someone to alleviate your problem. Capstone Project Writers is here for you.

    ReplyDelete
  14. This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the good work. I have been meaning to write something like this on my website and you have given me an idea.
    artificial intelligence course

    ReplyDelete
  15. This method involves the assessment of behavior in actual practice and also in game contexts. essay help

    ReplyDelete
  16. This write-up, I truly feel this website need far more; I’ll probably be back again to read through more thanks for the information shared. The article is really great for sharing. Also visit ecwa theological seminary form closing date

    ReplyDelete
  17. It’s a great nice time visiting your beautiful blog post. I really admire your blog. Thanks so much for sharing. Visit here renaissance university school fees schedule for direct entry

    ReplyDelete