Tutorial By Example project in Maven



Apache Maven is a nice tool for software project management, provides new way for developer to develop/manage/build/deploy the project artifact with help of project object model (POM) deployment descriptor file.

Requirement: I'm running this example in:
Operating System > Windows 7
Java > Java version: 1.7.0_25
Maven > Apache Maven 3.2.1
For more details see the Apache Maven

Apache Maven 3.3.9 installation




In this tutorials, we'll discussed about, how to create the hello world project for tutorial by example with help of Maven.

Note: Assumption you have install the java and maven.

Open the command prompt, you will get similar info
C:\>mvn --version
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T17:37:52+00:00)
Maven home: C:\apache-maven-3.2.1\bin\..
Java version: 1.7.0_25, vendor: Oracle Corporation
Java home: C:\java1.7\jdk1.7.0_25\jre
Default locale: en_GB, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

Creating the project:
Open the cmd prompt and type the as below:

C:\>mvn archetype:generate -DgroupId=com.tutorialbyexample -DartifactId=tutorialbyexample-app -DarchetypeArtifactId=maven-archetype-quickstart -Dinte
ractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ 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: groupId, Value: com.tutorialbyexample
[INFO] Parameter: packageName, Value: com.tutorialbyexample
[INFO] Parameter: package, Value: com.tutorialbyexample
[INFO] Parameter: artifactId, Value: tutorialbyexample-app
[INFO] Parameter: basedir, Value: C:\
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\tutorialbyexample-app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.770 s
[INFO] Finished at: 2015-03-09T11:21:57+00:00
[INFO] Final Memory: 9M/25M
[INFO] ------------------------------------------------------------------------

tutorialbyexample-app
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- tutorialbyexample
| `-- App.java
`-- test
| `-- java
| `-- com
| `-- tutorialbyexample
| `-- AppTest.java


pom.xml
<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>com.tutorialbyexample</groupId>
  <artifactId>tutorialbyexample-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>tutorialbyexample-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

App.java:
package com.tutorialbyexample;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

AppTest.java
package com.tutorialbyexample;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }

}

Build the Project:
It will build the jar file along with all dependency:

C:\>cd tutorialbyexample-app

C:\tutorialbyexample-app>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building tutorialbyexample-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ tutorialbyexample-app ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\tutorialbyexample-app\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ tutorialbyexample-app ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\tutorialbyexample-app\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ tutorialbyexample-app ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\tutorialbyexample-app\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ tutorialbyexample-app ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\tutorialbyexample-app\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ tutorialbyexample-app ---
[INFO] Surefire report directory: C:\tutorialbyexample-app\target\surefire-reports
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.pom (2 KB at 4.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom (3 KB at 23.4 KB/se
c)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.jar
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.jar (26 KB at 59.0 KB/sec)

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.tutorialbyexample.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.011 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ tutorialbyexample-app ---
[INFO] Building jar: C:\tutorialbyexample-app\target\tutorialbyexample-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.454 s
[INFO] Finished at: 2015-03-09T11:29:49+00:00
[INFO] Final Memory: 11M/26M
[INFO] ------------------------------------------------------------------------
'cmd' is not recognized as an internal or external command,
operable program or batch file.

C:\tutorialbyexample-app>

Run it:
C:\tutorialbyexample-app>java -cp target/tutorialbyexample-app-1.0-SNAPSHOT.jar com.tutorialbyexample.App
Hello World!

All about the simple project creation in Maven.

Issue:

C:\>mvn archetype:generate -DgroupId=com.tutorialbyexample -DartifactId=tutorialbyexample-app -DarchetypeArtifactId=maven-archetype-quickstart -Dinte
ractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ 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: groupId, Value: com.tutorialbyexample
[INFO] Parameter: packageName, Value: com.tutorialbyexample
[INFO] Parameter: package, Value: com.tutorialbyexample
[INFO] Parameter: artifactId, Value: tutorialbyexample-app
[INFO] Parameter: basedir, Value: C:\
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.818 s
[INFO] Finished at: 2015-03-09T11:16:26+00:00
[INFO] Final Memory: 9M/25M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.2:generate (default-cli) on project standalone-pom: Directory tutori
albyexample-app already exists - please run from a clean directory -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
'cmd' is not recognized as an internal or external command,
operable program or batch file.
C:\>

Solution: Add the below line like wise in your settings.xml located under \apache-maven-3.2.1\conf

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->

  <proxies>
    <proxy>
      <id>myproxy</id>
      <active>true</active>
      <protocol>http</protocol>
      <host>proxyhost</host>
      <port>proxy_port_8080</port>
      <username>your_user_name_if_any</username>
      <password>Password_if_any</password>
      <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
    </proxy>
  </proxies>

Please provide your input if any.

No comments:

Post a Comment