Maven
Maven is not only a build tool. it can do a lot more, but i tend to use it for that mostly so that is what this focuses on.
A simple quick guide can be found here
installation
As always i first attempt to take the simple apt-get, so
creating a project
Maven has a repository packed with possible solutions for problems, and mvn generate will list them all for you :
| generate archetype | |
|---|---|
Try it, at the time of writing it went all the way down to 839 solutions. And after that it suggests that i choose # 336 :
| error | |
|---|---|
Why not, and why not take the default in the next options as well, it suggests the newest version. But after that you need to provide some real input.
- groupId : this is a unique name, and resolves to a working group. Maybe a department of a company. The quick guide's example is -DgroupId=com.mycompany.app. So i will choose for org.klopt.sandbox.
- artifactId : this resolves roughly to a module or application name. The quick guide named this -DartifactId=my-app. So i called it maventest
- version : a string representing the version. hey man, i just started out so 0.1 for me
- package : maven already fills in the groupId here, so i will go with that : org.klopt.sandbox
After a final confirmation, you will have a directory structure called /maventest'. It looks like this :
maventest/
├── pom.xml
└── src
├── main
│ └── java
│ └── org
│ └── klopt
│ └── sandbox
│ └── App.java
└── test
└── java
└── org
└── klopt
└── sandbox
└── AppTest.java
Pivot in this is the pom.xml file. It looks like this :
To use it you can now run 'goals' using this file. Since we installed 336: A sample application, also your source files App.java And AppTest.java contains some working code, albeit minimal :
| example | |
|---|---|
This is the main file App.java. AppTest.java is used for unit testing and contains some more functions. But to build them both, run :
| maven package | |
|---|---|
It will create a new tree called 'target' which contains your newly built classes. Run with :
| run command | |
|---|---|
Luckily bash command completion (tab) helps with the class hierarchy string.
** maven goals / phases
The 'mvn package' command is called a goal, or phase or life cycle. Here are a couple more useful goals.
| clean | |
|---|---|
Removes the target tree and returns to the begin state.
Just generates the App.class tree, so target.classes.org.klopt.sandbox.App.class.
| test | |
|---|---|
Runs the tests in AppTest.java and also builds that first if needed.
| install | |
|---|---|
Install the package into the local repository, for use as a dependency in other projects locally. In my case it ended up in "/home/kees/.m2/repository/"
| deploy | |
|---|---|
Done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
Nice one: generates a website containing the documentation for instance. The site will be under 'target/site'.
For a lot of these, you will need to have some patience the first time, because everything needed is downloaded at first use.