Wednesday, July 30, 2014

IDEA friendship with Maven project from scratch

Introduction

Create your Maven project with the common standard folder structure from scratch and import it into Intelij IDEA in 10 min.

Preconditions
If u have already installed JDK and Maven you can skip this part.

Installing JDK (Windows)

  • Download jdk and launch installer;
  • Add or edit JAVA_HOME Environment variable:
    • Go to Control Panel -> System -> Advanced -> Environment Variables;
    • Add or edit JAVA_HOME variable to correspond your JDK path:
  • Update PATH Environment variable:
    • Add the location of the JDK 'bin' folder to the PATH variable in 'System Variables' (same window as above):

      U can also use %JAVA_HOME%\bin instead of full path.
  • Open command line (Ctrl+R) and run 'java -version' command to verify
    if everything is correct:
Note: If you do not set the PATH variable, you should specify the full path to the executable file every time you run it, such as:C:\> "C:\Program Files\Java\jdk1.8.0\bin\javac" MyApp.java

Installing Maven (Windows)

  • Download Maven and unzip archive to some directory.
    In this case: C:\Program Files\Apache Software Foundation\apache-maven-3.2.2.
  • Add or edit JAVA_HOME Environment variable:
    • Go to Control Panel -> System -> Advanced -> Environment Variables;
    • Add or edit M2_HOME variable to correspond your unpacked folder:
  • Update PATH Environment variable:
    • Add the location of the Maven 'bin' folder to the PATH variable
      in 'System Variables':
  • Open command line(Ctrl+R) and run 'java -version' command to verify 
    if everything is correct:

Create Maven Project

Create a directory for your project somewhere and start a shell in that directory. On your command line, execute the following Maven goal:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=some-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Under this directory you will find standart maven project structure.
In my case command was following:
D:\_Personal\_JAVA\java_repo>mvn archetype:generate -DgroupId=com.cleverworld.whatsup -DartifactId=whatsup-world -DarchetypeArtifactId=maven-archetype-quickstart-DinteractiveMode=false
So i had:
whatsup-world
  |-- pom.xml
  `-- src
        |-- main
        |    `-- java
        |         `-- com
        |               `-- cleverworld
        |                     `-- whatsup
        |                            `-- WhatsupApp.java
        `-- test
             `-- java
                  `-- com
                        `-- cleverworld
                              `-- whatsup
                                    `-- WhatsupAppTest.java
Build project
mvn package
If everything Ok, command line will print some info end with something like:
[INFO] Building jar: D:\_Personal\_JAVA\java_repo\whatsup-world\target\whatsup-world-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.018 s
[INFO] Finished at: 2014-07-30T16:41:06+03:00
[INFO] Final Memory: 14M/77M
[INFO] ------------------------------------------------------------------------
You may test newly compiled and packaged JAR with the following command:
java -cp target/whatsup-world-1.0-SNAPSHOT.jar com.cleverworld.whatsup.WhatsupApp
Result:
What's up World!

Make friends with IDEA

Just go to File -> Import Project, choose the pom.xml in the root directory of your project and enjoy the result:
BTW: I believe you had Maven plugins enabled:)...
  • Add your project under version control. 
  • Don't forget to add 'target' & '.idea' folders and the '*.iml' file to the ignore list. 
  • It will be nice to make the project description file as well.


Well done!
It had taken much more time to write this article than to create Maven project:)
So, i hope it was useful for u. Bye ;)

No comments:

Post a Comment