Apache Maven is a project management and build tool for Java developers. It handles several tasks including:
- Dependency management: when you use versioned libraries, Maven can pull the appropriate versions from the cloud.
- Build management: Maven can compile your program and all dependencies.
- Project metadata: Maven can assign information about your project when it compiles your program(s).
- Program execution: Maven will allow you to run your code and/or test cases.
A Maven project is specified in a file called pom.xml
in the root directory of the project.
By default, the project is laid out as follows:
src/
represents the source files to be compiled-
src/main/java
represents Java source -
src/main/resources
represents files that are copied into the same directory as the Java project at compile time -
src/test/java
represents source files that are only used as test cases duringmvn test
-- typically these are JUnit tests. -
src/testresources
represents files that are copied into the same directory as the Java project at test time.
-
target/
represents the compiled code. Normally this is created as needed. You can runmvn clean
to delete the target.~/.m2/repository
is where Maven downloads versioned dependencies.
When you run Maven, you typically do this with a goal. Some common goals include:
compile
-- compile main sourcetest-compile
-- compile test casestest
-- run test casesexec
(with additional options) -- run source codepackage
-- generate a jar package of your code (and optionally all dependencies)install
-- copies the jar file into~/.m2/repository
after building and creating it
When you run mvn compile
, all of the libraries your program depends upon should be downloaded to a directory called .m2
in your home directory. Then your program should get compiled to target/classes
inside your project directory. Any files in src/main/resources
will also be copied into target
.
When you run mvn exec:java
, it will run the main class (from the pom.xml
) from the target/classes
directory, also adding all dependencies to the class path.
When you run mvn test-compile
, similarly to how mvn compile
generates classes in target/classes
, you will get additional classes in target/test-classes
. (These will be your src/test/java
classes.) Again, anything from src/test/resources
will get copied in.
When you run mvn test
, this will run all test cases from target/test-classes
. Due to a special Maven plugin, the results will get summarized in target/surefire-reports
.
If you run mvn clean
, this will delete the target
subdirectory and all of its content.
When you run mvn exec
, Maven first tries to determine what to run (often, Java, as in mvn exec:java
). Next it looks in the pom.xml
file for the exec-maven-plugin
. Within the related XML, there should be a definition of the goal
(named as the ID) and the mainClass
. You can see this illustrated here:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>loader</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>edu.upenn.cis.nets2120.hw1.LoadData</mainClass>
<arguments>
</arguments>
</configuration>
</execution>
</executions>
</plugin>