Starting a Java project with Maven 2From WikiJavabuy this book
the articleGetting StartedIn it's essence Maven is a command line build tool that can compile and aggregate all the bits and pieces of your project. It runs unit tests automatically straight out of the box and has a very convenient dependency management system. After you have downloaded and followed the installation instructions in the download page open a command window (if you are using Windows) or bring up your Linux console and enter in this command: mvn -v If the instructions have been followed correctly, then the consoles output should be something like this: Maven version: 2.0.7 Java version: 1.4.2_16 OS name: "windows xp" version: "5.1" arch: "x86" Since Maven relies on the projects folder structure, w have to create the structure first before we start adding our source and resource files. To do so:
mvn archetype:create \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DgroupId=com.yourcompany.app \ -DartifactId=my-project After the command has executed, you should see something like this in your command interface: [INFO] Parameter: groupId, Value: com.yourcompany.app [INFO] Parameter: packageName, Value: com.yourcompany.app [INFO] Parameter: basedir, Value: C:\wikijava [INFO] Parameter: package, Value: com.yourcompany.app [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: artifactId, Value: my-project [INFO] ********************* End of debug info from resources from generated POM *********************** [INFO] Archetype created in dir: C:\wikijava\my-project [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 15 seconds [INFO] Finished at: Mon May 26 14:26:59 BST 2008 [INFO] Final Memory: 4M/8M [INFO] ------------------------------------------------------------------------ Note: If you are using Windows, then the line brakes in the command above have to be removed since the Windows console doesn't support commands with line brakes in them. When the above command has executed successfully navigate to the folder that Maven has created. In this case it's my-project and execute the command: mvn installThis command and mvn cleanwill be the most used commands while working with maven. The install command (or goal in build tool terms) compiles your source code, runs all the unit tests it can find and aggregates the class files into a jar, war or ear file (this can be specified by modifying the Maven pom.xml (Project Object Model) file). When running the command for the first time don't be surprised if takes a few minutes to complete. This is because Maven does most of the dependency management itself. While the command is executing you will see many libraries being downloaded. These libraries and plugins are stored locally in your local Maven repository, so the next time you will run this command Maven won't need to download everything again. If you can see a[INFO] BUILD SUCCESSFULin the console after the install command has finished, then this means that the project was successfully built.
Importing a Maven project into EclipseIf you are using Eclipse as your development IDE, you probably will want to get the project imported in it. To do so:
That's it, your project with all the dependencies and references is imported in Eclipse and you are good to go on your next project. Have fun!
|
