Maven Quick Start

Source: Internet
Author: User
Tags sonatype

What is Maven.

I. Installation
JDK must be installed first.
Set java_home = D: \ Java \ JDK \ jdk1.7.0
Set Path = % java_home % \ bin; % PATH %

Http://maven.apache.org/

Set m2_home = D :\java \ Server \ apache-maven-3.0.5
Set Path = % m2_home % \ bin; % PATH %

Check whether the installation is successful
Echo % java_home %
Java-version

Echo % m2_home %
MVN-V

Ii. Configuration
Best practices
Copy % m2_home %/CONF/settings. xml ~ /. M2/settings. xml
Modify the location of localrepository. The default value is ~ /. M2/Repository
% M2_home %/CONF/settings. xml
<! -- Localrepository
| The path to the local repository MAVEN will use to store artifacts.
|
| Default :~ /. M2/Repository
<Localrepository>/path/to/local/repo </localrepository>
-->

Iii. General commands
MVN help: the system takes a long time.
MVN help: valid-Pom view valid POM

4. Start Project Creation
Create a Java Project
MVN archetype: generate-dgroupid = com. Company. Project-dartifactid = module-darchetypeartifactid = Maven-Archetype-Quickstart
Create a WEB Project
MVN archetype: generate-dgroupid = com. Company. Project-dartifactid = web-darchtypeartifactid = Maven-Archetype-webapp
To create a naked project, select
MVN archetype: generate
MVN archetype: generate-darchetypecatalog = internal is faster
Convert to eclipse Project

MVN Eclipse: Eclipse

Install the Eclipse plug-in and use Windows to create it.

V. Eclipse plug-in
GEF (required) http://download.eclipse.org/tools/gef/updates/releases/
M2eclipse http://m2eclipse.sonatype.org/sites/m2e/
SVN integrated (enhanced) http://m2eclipse.sonatype.org/sites/m2e-extras/
During installation, note that if it cannot be installed, do not install it first.
After installation, eclipse will change the meven location in the preferences.

6. another batch of Common commands
MVN clean compile;
MVN clean test;
MVN javadoc: javadoc;
MVN clean package;
MVN clean install; install locally
MVN clean deploy; install it in private server or Central Library
Dependent commands
MVN dependency: Analyze
MVN dependency: List
MVN dependency: Tree

VII. Project Structure

Parent Project, Child project, and web project are separated.

This must be learned and noticed that the generated web project does not contain src/Main. Why is it because we encourage the establishment of modules?

For example, for a project, core is a module, service is a module, Web is a module, desktop is a module, and Web and desktop share core and service

Let's take a look at the spring package.

Create Parent and Child projects

CD/d:/Java/workspace
MVN archetype: Create-dgroupid = com. Company. Project-dartifactid = project-parent
Modify <packaging> jar </packaging> in POM. XML to <packaging> pom </packaging> and delete SRC
CD project-parent
Create a jar Project
MVN archetype: Create-dgroupid = com. Company. Project-dartifactid = p-service-dpackage = com. ebiz. Project. Service-darchetypeartifactid = Maven-Archetype-Quickstart
Create a WEB Project
MVN archetype: Create-dgroupid = com. Company. Project-dartifactid = p-web-dpackage = com. ebiz. Project. Web-darchetypeartifactid = Maven-Archetype-webapp
Open Pom. xml and add the following parts
<Modules>
<Module> P-service </module>
<Module> P-Web </module>
</Modules>
Convert to eclipse Project
MVN Eclipse: Eclipse

It is also very easy to have src/main and src/test in the web project. Open. classpath and manually add

<Classpathentry kind = "src" output = "target/classes" Path = "src/main/Java"/>
<Classpathentry excluding = "**" kind = "src" output = "target/classes" Path = "src/main/resources"/>
<Classpathentry kind = "src" output = "target/test-classes" Path = "src/test/Java"/>
<Classpathentry excluding = "**" kind = "src" output = "target/test-classes" Path = "src/test/resources"/>

8. Warehouse
. M2-> private server-> central warehouse (or private server)

IX. Resources
Http://search.maven.org/is a good place to find Jar packages, even without Maven
Http://juvenshun.iteye.com/Maven practice

10. Private servers

Http://www.sonatype.org/nexus/

Download and decompress the package, and then upload the CD.
Run the Nexus Console
Install nexus install as a Windows Service
Net start | stop nexus manual START | stop the service

Best practices
Configure a hosts (c: \ windows \ system32 \ drivers \ etc \ hosts)
IP of nexus service nexus
In this way, if the private server is changed, you only need to change the hosts to switch between them.
Access through http: // nexus: 8081/nexus
Enter admin/admin123. If you are on the Internet, change the password.
Repositories/Central/configuration/download remote indexes-> true
~ /. M2/settings. xml configuration
<Server>
<ID> releases </ID>
<Username> admin </username>
<Password> admin123456 </password>
</Server>
<Server>
<ID> snapshots </ID>
<Username> admin </username>
<Password> admin123456 </password>
</Server>

<Profile>
<ID> nexus </ID>
<Repositories>
<Repository>
<ID> Public </ID>
<Name> local private nexus </Name>
<URL> http: // nexus: 8081/nexus/content/groups/Public </URL>
<Releases>
<Enabled> true </enabled>
</Releases>
<Snapshots>
<Enabled> false </enabled>
</Snapshots>
</Repository>
<Repository>
<ID> Public-snapshots </ID>
<Name> local private nexus </Name>
<URL> http: // nexus: 8081/nexus/content/groups/public-snapshots </URL>
<Releases>
<Enabled> false </enabled>
</Releases>
<Snapshots>
<Enabled> true </enabled>
</Snapshots>
</Repository>
</Repositories>

<Pluginrepositories>
<Pluginrepository>
<ID> Plugin-Public </ID>
<Name> local private nexus </Name>
<URL> http: // nexus: 8081/nexus/content/groups/Public </URL>
<Releases>
<Enabled> true </enabled>
</Releases>
<Snapshots>
<Enabled> false </enabled>
</Snapshots>
</Pluginrepository>
<Pluginrepository>
<ID> Plugin-public-snapshots </ID>
<Name> local private nexus </Name>
<URL> http: // nexus: 8081/nexus/content/groups/public-snapshots </URL>
<Releases>
<Enabled> false </enabled>
</Releases>
<Snapshots>
<Enabled> true </enabled>
</Snapshots>
</Pluginrepository>
</Pluginrepositories>
</Profile>

<Activeprofiles>
<Activeprofile> nexus </activeprofile>
</Activeprofiles>

11. Plug-ins. Everything can be plug-ins

Http://search.maven.org/search engine g: "org. Apache. Maven. plugins" is officially more than 59

There is nothing unexpected to do. What Google do you want.

12. MAVEN and tomcat
Maven-Tomcat-plugin works well with Maven and tomcat. It can deploy applications to the Tomcat server, or start Tomcat as an Embedded Server, just like jetty.
Use JPDA to start Tomcat remote debugging. In this way, you can work with eclipse to easily implement debugging. It also has the hot deployment function of Tomcat.
 
The procedure is as follows:
1. Configure the maven-Tomcat-plugin plug-in the Pom. xml file.
XML Code
<Plugin>
<Groupid> org. codehaus. Mojo </groupid>
<Artifactid> tomcat-Maven-plugin </artifactid>
</Plugin>
 
2. Configure the environment variable maven_opts and start the debugging function.
Write
Maven_opts =-xdebug-xrunjdwp: Transport = dt_socket, address = 1044, Server = Y, suspend = N
 
3. configure remote debugging in eclipse

Maven command: MVN Tomcat: Run

Reference: This is the focus

Http://mojo.codehaus.org/tomcat-maven-plugin/introduction.html

The above configuration is to use Tomcat as an Embedded Server.

If Tomcat is used as an external server, the following configuration is also required.
1. Modify startup. bat.
Modify call "% executable %" JPDA start % cmd_line_args %
2. Modify Catalina. bat
Modify set jpda_transport = dt_socket
Modify set jpda_address = 1045
3 modify tomcat-users.xml
If not, add <role rolename = "manager"/>
Add, <User Username = "admin" Password = "" roles = "manager"/>
By default, Maven uses the admin password to log on to the Tomcat console.

13. Scope on which the package depends
* Compile, default value, applicable to all stages and will be released along with the project.
* Provided, similar to compile, expects JDK, container, or user to provide this dependency. For example, Servlet. jar.
* Runtime, used only at runtime, such as the JDBC driver, applicable to the running and testing stages.
* Test, which is only used for testing and is used to compile and run the test code. Will not be released along with the project.
* System, similar to provided, must explicitly provide jar containing dependencies. MAVEN will not find it in repository.

For system,
* You can configure the absolute path locally,
* If a web project is better placed in the lib directory, then <systempath >$ {project. basedir}/src/main/webapp/WEB-INF/lib/xxx. Jar </systempath>
<Dependency>
<Groupid> org. Apache. Tools </groupid>
<Artifactid> zip </artifactid>
<Version> 1.0 </version>
<Scope> System </scope>
<Systempath> $ {project. basedir}/src/main/webapp/WEB-INF/lib/apache-ant-zip.jar </systempath>
</Dependency>
* It can be released to a local private server. The second type is recommended.

Package dependency Optimization Principles
As few as possible, do not reference unused packages
Test> provided> runtime> compile
View dependencies using plug-ins

14. doploy precautions

1. The release library must allow redoploy
2. Enable cental Library
3. If deploy has a parent project, you must deploy the parent project. Otherwise, problems may occur.

CD/d: \ Java \ workspace \ Maven \ SSM-core
MVN deploy: deploy-file-dgroupid = cn. kimsoft-dartifactid = SSM-core-dversion = 1.1.1-dpackaging = jar-dfile = target/ssm-core-1.1.1.jar-durl = http: // localhost: 8081/nexus/content/repositories/Central/-drepositoryid = nexus
MVN deploy: deploy-file-dfile = target/ssm-core-1.1.1.jar-durl = http: // 127.0.0.1: 8081/nexus/content/repositories/releases/-drepositoryid = nexus-release

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.