Install Maven
This post is continued from Getting Started with Java's Hello World: Part I. In this section, we will download Maven 3 for Windows. Maven is typically used to compile Java programs. I googled "download Maven for Windows", clicked on Download - Maven - Download Apache Maven (Apache), went to the Maven 3.1.0 Section, clicked on apache-maven-3.1.0-bin.zip.- Download the zip file to your Desktop.
- Extract the file to "C:\".
- Open a command prompt.
- Type the following command:
"C:\apache-maven-3.1.0\bin\mvn"
- Run->Type "sysdm.cpl"->Advanced Tab->Environment Variables->System Variables Section.
- New->Enter "JAVA_HOME" as the name and "C:\Program Files\Java\jdk1.7.0_25" as the value.
- Select the Path variable->Edit.
- Use the error keys to move to the end of the value field and add ";C:\apache-maven-3.1.0\bin" (Make sure the first character is a semicolon, so that you can run maven from the command line, so you don't get a "mvn is not a recognized command" error).
- Click OK to close the System Properties Control Panel.
- Close the existing command prompt.
- Open a new command prompt.
- Type the following command:
mvn -version
Create Your Maven Project
In this section we will create a Java Project. We will use Maven to compile this project from the pom.xml file. See http://maven.apache.org/guides/getting-started/ for more details.- Open a command prompt.
- Create a workspace directory (ie Type: mkdir "C:\Users\Scott\workspace").
- Move to your workspace directory (ie Type: cd "C:\Users\Scott\workspace").
- Type the following command:
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=my-app
- When you see "Choose a number or apply filter", Hit "Enter".
- When you see "Choose a number", type "6" and Hit "Enter".
- When you see "Define value for property", type "1.0" and Hit "Enter".
- When you see "Y", type "Y" and Hit "Enter".
- This will download a bunch of libraries (jars) to the Local Maven Repository, ie "C:\Users\Scott\.m2\repository"
- This will also create the my-app project (ie "C:\Users\Scott\workspace\my-app\pom.xml")
- This will also create a src directory (ie "C:\Users\Scott\workspace\my-app\src")
- Type: cd my-app
- Type: mvn clean
- This will clean the project directory.
- You should see BUILD SUCCESS.
- This command looks for the pom.xml file which is used to manage files for a Java Project
- Type: mvn compile
- This will compile the project.
- You should see BUILD SUCCESS.
- This will create compiled Java class files (ie "C:\Users\Scott\workspace\my-app\target\classes").
- You should be able to run the compiled Java class file: (ie to see "Hello World!" displayed, Type: java -classpath "C:\Users\Scott\workspace\my-app\target\classes" com.mycompany.app.App).
- Type: mvn test
- This will run tests in the project (currently 1 test).
- You should see BUILD SUCCESS.
- Type: mvn eclipse:eclipse
- This will create Eclipse project files.
- You should see BUILD SUCCESS.
- Type the following command:
mvn eclipse:add-maven-repo -Declipse.workspace="C:\Users\Scott\workspace"
- Replace "C:\Users\Scott\workspace" with your workspace.
- This sets up a Java classpath variable which allows Eclipse to find the local Maven repository.
This post was reposted from http://scottizu.wordpress.com/2013/08/14/getting-started-with-javas-hello-world-part-ii/, originally written on August 14th, 2013.
No comments:
Post a Comment