Introduction
This article describes how to compile Java and web projects through Maven plug-ins in eclipse.
Install Maven
To download the latest version of Maven, see maven.apache.org/download.html.
The current version 2.0.7.
Decompress the downloaded file and set the bin directory to the Windows PATH environment variable.
Test whether the installation is successful: Enter
- MVN-version
Install Maven plug-in
Install the maven plug-in m2eclipse of Eclipse.
This article uses:
- JDK: Sun jdk1.6.0 _ 03
- Eclipse: 3.3.1.1
M2eclipse plug-in Web site: http://m2eclipse.codehaus.org
Update Web site online via Eclipse: http://m2eclipse.sonatype.org/update/
For detailed installation steps, see the plug-in website.
Set the classpath variable for Maven and Java> build path> classpath Variables
Use the new... button to add a variable entry:
- Name: m2_repo
- Path: C:/Documents and Settings/zhangsan/. m2/Repository, zhangsan is the user name in your XP
Create a Maven Java project and import it to eclipse
In the command line, go to the eclipse workspace directory and enter:
- MVN archetype: Create-dgroupid = org. Marshal-dartifactid = helloworld
Where:
- Org. organization Al is the name of the organization, and Maven automatically generates the source code package ORG/organization Al
- Helloworld is the project name
After running the command, the helloworld directory is generated under the workspace directory and generated:
- Project Build File: Pom. xml
- Code framework: src/main/Java/org/marshal/helloworld/APP. Java
- Test code: src/test/Java/org/marshal/helloworld/apptest. Java
In this case, you can use the text editor to modify the app. Java source code and compile and package it with the following command:
- Maven pacage
After compiling apptest. Java, run the following command to run the JUnit test:
- MVN Test
Run the main method using the following command:
- Java-CP target/helloworld-1.0-SNAPSHOT.jar org. Marshal. app
However, it is better to write and debug code using ide tools, which requires importing Maven projects into eclipse projects.
In the helloworld directory, run the following command to make the project support Eclipse:
- MVN Eclipse: Eclipse
Then, open eclipse and import helloworld through File> Import..., General> existing projects into workspace.
After the import, for example:
Create a Maven web project and import it to eclipse
To create a Maven web project, run:
- MVN archetype: Create-dgroupid = org. Marshal-dartifactid = myweb-darchetypeartifactid = Maven-Archetype-webapp
A web project named myweb will be created.
Add eclipse WTP support for this project to the myweb directory and run:
- MVN-dwtpversion = 1.0 Eclipse: Eclipse
Import the project to eclipse, similar to the Java project above.
Use m2eclipse plug-in
After the m2eclipse plug-in is successfully installed, click the web project or Java project you just created in eclipse.
Right-click Maven> enable dependency management to make the m2eclipse plug-in take effect for this project.
Right click again and select Maven> Add dependency to add the required class library through the network.
See:
After the selected version is selected, Maven automatically downloads the version of the Class Library to the local device and automatically adds it to the eclipse class library.
Note: When the maven plug-in is installed, there is always a prompt like this:
Eclipse is running in a JRE, but a JDK is required
Some Maven plugins may not work when importing projects or updating source folders.
There are two solutions:
1. Add VM startup parameters in eclipse. ini to create the VM used for eclipse startup. The modified file content is as follows:
-Showsplash
Org. Eclipse. Platform
-- Launcher. xxmaxpermsize
256 m
-VM
C:/program files/Java/jdk1.5.0 _ 12/bin/javaw.exe (write the bin/javaw.exe directory in the JDK path you have installed here)
-Vmargs
-Dosgi. requiredjavaversion = 1.5
-Xms40m
-Xmx512m
2. Create a shortcut, right-click the shortcut attributes, and enter: D:/Eclipse/eclipse.exe-vm c: /program files/Java/jdk1.5.0 _ 12/bin/javaw.exe
D: path of the eclipse.exe file in/Eclipse/eclipse.exe
C:/program files/Java/jdk1.5.0 _ 12/bin/javaw.exe: Bin/javaw.exe under the JDK path you have installed
Configuration Information prompt:
1. When running some Maven commands for the first time, MAVEN will definitely download some required components from the Internet,
Some users are dumpfounded (for example, me) after the firewall is opened. Open/CONF/setttings. XML in the maven decompress directory,
Locate the proxies line and remove the <proxy> </Proxy> comment. Then everyone knows how to change it.
2. The components downloaded by Maven are stored in your user directory by default, which is inconvenient to manage. Or open settings. XML,
Find the <localrepository> section and remove its comments. You can also see how to change it. For example
Yes <localrepository> D:/netbeans5.5/nb5.5/m2-repository </localrepository>
Hey!