Sunday, November 10, 2013

Developing a Sample HelloWorld Java Program by using Maven

Step1: Create a HelloWorld java project by using mvn command.

[ranga@ranga Documents]$ mvn archetype:generate -DgroupId=com.ranga -DartifactId=HelloWorld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Project Structure looks like

HelloWorld
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── ranga
    │               └── App.java
    └── test
        └── java
            └── com
                └── ranga
                    └── AppTest.java

9 directories, 3 files

Step2:

edit the pom.xml and add the following line to run a application  

<build>
    <plugins>
        <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
                <source>1.6</source>
                <target>1.6</target>
        </configuration>
     </plugin>
        <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <configuration>
             <mainClass>com.ranga.App</mainClass>
        </configuration>
        </plugin>
    </plugins>
   </build>
 
Step3: Run the following command to compile and run. 

[ranga@ranga HelloWorld]$ mvn compile exec:java

Hello World!






0 comments: