Compiling and Running
In the same folder where we created the project, run this commandObject to compile the project
mvn compile
Maven will create a directory target which contains all compile classes, Jar files etc.
There are various ways to Run the Java main method. We can go to the target directory and run it with standard java
command. Another quick maven
way is to use plugin Exec Maven Plugin.
Just run this command in the root folder, maven will download the
plugin(if it's the first time) and execute the main method
mvn exec:java -Dexec.mainClass="com.logicbig.App"
It will generate this ouput:
D:\maven-first-example>mvn exec:java -Dexec.mainClass="com.logicbig.App"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-first-example 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ maven-first-example ---
Hello World2!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.304s
[INFO] Finished at: Wed Oct 28 22:05:39 CDT 2015
[INFO] Final Memory: 8M/85M
[INFO] ------------------------------------------------------------------------
OK, so far not a big deal!! But now we know how to start a project with Maven. We will see in upcoming tutorials
the real power of Maven when we will use external dependencies and more.
Understanding Maven Directory Structure
Maven has a Standard
Directory Structure
In our example, we saw App.java is under src/main/java. So our Java src files will always go there. That's one of
the maven defaults. We can
change the src directory but we have to override the default. How can we do that? There comes pom.xml. All
default configuration can be overridden there. pom.xml is the heart of the maven project. This is the only mandatory
file to run any mvn command. The standard directory structure is not defined by a specific Archetype. Archetype
implementation have to follow Maven standard directory
structure or they are also free to override the defaults. Archetype plugins actually are there to automate the
project creation.
In our example we saw AppTest.java is under src/test/java which is another default for test source files. There are
more default directories not created by the Archetype we used. For example src/main/resources. If we need to use
some resources e.g. properties file, we can extend our project and add resources directory manually or we can look
for some
other Archetype plugin.
Importing Maven project into IDE
Maven is not dependent on any IDE but each major IDE provides Maven support. For example you can import the same project
into
Intellij(File->New->Project from Existing sources..) or even you can create the new Maven project from within Intellij.
One of advantages of using an IDE is, we can execute java main methods directly from IDE without running maven command,
in those cases the IDE is responsible to resolve dependencies etc defined in pom.xml
In our examples we won't be focusing on any IDE. The idea is we should learn new concepts in the most generic/original
way so
that we will be able to work in all sort of environments.
|