MVN tool Introduction

Source: Internet
Author: User

When I learned about Mina, I found that Mina uses Maven for project management. Start learning Maven again, and take notes during this period. To learn this build tool similar to ant and more advanced than ant, you must first master the parameters under the MVN command, and then combine these MVN and parameters for practical use in the project.

MVN divides the project into five phases (lifecycle): preparation, compilation (complie), testing, deployment, and running

Download configuration Maven:
 

Detailed procedures are available online.

1.1 user
Configuration of the specified user. User configuration can be specified in $ {user. Home}/. m2/settings. xml. Note: This file is not required. When the file cannot be found, Maven uses the default configuration.

The specific configuration of this file can refer to: http://maven.apache.org/ref/2.0.4/maven-settings/settings.html

1.2 configure proxy
In maven2.0, the proxy server accesses the Internet and can configure a proxy for HTTP request. Similarly, in user configuration, the configuration is as follows:

<Proxy>
<ID> optional </ID>
<Active> true </active>
<Protocol> HTTP </protocol>
<Username> proxyuser </username>
<Password> proxypass </password>
<Host> www.hc360.com <Port> 80 </port>
<Nonproxyhosts> local.net, some.host.com </nonproxyhosts>
</Proxy>

See: http://maven.apache.org/guides/mini/guide-proxies.html

1.3 security and deployment configuration
The repository to be used in a project is specified in setting. xml. However, you cannot

Code and other security settings are also placed in the project. Therefore, you may define a server in your own settings and specify an ID for it.
Corresponds to the repository that the project will use.

In addition, some repository may require the user name and password during download, which can be specified in the same way in server element. The configuration is as follows:
<Server>
<ID> deploymentrepo </ID>
<Username> repouser </username>
<Password> repopwd </password>
</Server>

1.4 mavenplugins for eclipse
The URL installed through eclipse is a http://m2eclipse.sonatype.org/update/ and can be queried on the official website.

Configuration: In window --> references --> JAVA --> build path --> classpath variables, press "new" and name: m2_repo to create a directory pointing to the current resource library: C: /Documents and Settings/pengch /. m2/Repository

Note: Set the name of the maven main directory to m2_home. The default jar library downloaded from the server is: $ {user. home }/. m2/Repository/, my $ {user. the value of home} is C:/Documents and Settings/administrator. The initial configuration is available in the: % m2_home %/bin/m2.conf file. If you want to modify the resposity value:

There are two methods: % m2_home %/CONF/settings. the fields in XML are: <localrepository> actual physical address </localrepository>, for example, <localrepository> E:/Maven /. m2/Repository </localrepository>

Another method is to use artifactory to create the local repository of Maven. I have never tried it. (From: http://www.javaeye.com/topic/98480)

The official Maven repository server is very slow. Do you still remember the last submarine cable Accident? The official server could not be accessed at all, and it was crazy during that time. In addition, the official server class library version is usually not the latest, and sometimes you have to do it yourself if you want to use the latest version. (The following describes how to manually create a local code server.

The Maven server is simple, just a common HTTP server, such as Apache.

Copy the folder of your local code library to the htpdocs/maven2 directory under the Apache installation directory. The Windows user is C:/Documents and Settings/Jeffrey /. m2/repository, which is/home/Jeffrey/in Linux /. m2/Repository (note. m2 is a hidden folder in Linux)

Start the Apache server and enter http: // localhost/maven2/repository in the browser. You can view the repository file directory.

Edit the project POM to specify the local code library Server

Add:

XML Code

<Repositories>
<Repository>
<ID> mylibsid>
<URL> http: // localhost/maven2/repositoryurl>
Repository>
Repositories>
 

Multiple repository records can be added. MVN searches in sequence. If none of them are found, MVN searches for the official server. (Completed)

Maven common command combinations:
Create: MVN archetype: Create-dgroupid = com. oreilly-dartifactid = app

Folder structure:

App

--- Com

------ Oreilly

Create a normal Java project for Maven: MVN archetype: Create-dgroupid = com. codeline. commons-dartifactid = pjoname
Create a Maven WEB Project: MVN archetype: Create-dgroupid = com. mycompany. app-dartifactid = My-webapp-darchetypeartifactid = Maven-Archetype-webapp

Compile:
CD app;

MVN compile

Compile the test case: compiling test sources and running unit tests

MVN test;

MVN test will always run the compile and test-compile phases first, as well as all the others defined before it.

Package,
Packaging and installation to your local repository

Making a jar file is straightforward and can be accomplished by executing the following command:

C:/APP> MVN package

Add the generated file to the local class library
Now, you'll want to install the artifact (the JAR file) You'll have generated into your local repository. it can then be used by other projects as a dependency. the directory <user_home> /. m2/Repository isthe default location of the repository.

To install, execute the following command:

C:/APP> MVN install

The file directory of the maven code library is organized. The same jar has an independent folder for each version to store. Besides jar, the description file of this jar is also available. POM and their verification files. sh1

The. Pom file defines the name and version of the jar and other jar files that the jar depends on (that is why a jar is introduced but a lot of jar files are generated)

The simplest Pom is as follows:

XML Code

XML version = "1.0" encoding = "UTF-8"?>
<Project>
<Modelversion> 4.0.0modelversion>
<Groupid> net. jeffreygroupid>
<Artifactid> xxxartifactid>
<Version> Version 1.0>
<Dependencies>
Dependencies>
Project>
Generate a verification file for jar and POM

You can use an ant task to do the following:

XML Code


<Target name = "checksum" Description = "generate checksum file for jar and pom">
<Checksum algorithm = "Sha" fileext = ". sha1">
<Fileset dir = "D:/javalib" id = "ID">
<Include name = "**/*. pom"/>
<Include name = "**/*. Jar"/>
<Include name = "**/*. xml"/>
<Exclude name = "**/*. sh1"/>
</Fileset>
</Checksum>
</Target>
 

Copy the generated sh1 files, Jar files, and pom files to the Apache code library according to the structure layout described above. Of course, you can copy them directly to the local code library.

(From: http://fluagen.blog.51cto.com/146595/40086)

When installing the hibernate package, tell me that JTA cannot be downloaded. If local installation is required, the following prompt is displayed:
1) javax. Transaction: JTA: jar: 1.0.1b
Try downloading the file manually from: http://java.sun.com/products/jta.Then, install it using the command:
MVN install: Install-file-dgroupid = javax. Transaction-dartifactid = JTA/-dversion = 1.0.1b-dpackaging = jar-dfile =/path/to/filepath to dependency:
1) COM. efn: mywebapp: War: 1.0-Snapshot
2) org. hibernate: jar: 3.1rc2
3) javax. Transaction: JTA: jar: 1.0.1b
----------
1 required artifact is missing. for artifact: COM. efn: mywebapp-1.0-SNAPSHOT.war this prompt is that you can go to the Sun site to download the JTA rack package, and then use the command line to install as required, because I would have this rack package, copy the file to a convenient location, such as C:/, and then enter:
MVN install: Install-file-dgroupid = javax. Transaction-dartifactid = JTA-dversion = 1.0.1b-dpackaging = jar-dfile = C:/JTA. Jar
After the execution is complete, everything is OK!

(Completed)

Other commands:
Maven relies on this POM to create a website for your project without any additional work. You only need to customize your Maven website. If time is short, you only need to execute the following command:

MVN site
 

There are also many goals that can be executed, such

MVN clean
 

This command deletes the target directory.

You may need to generate an intellij idea descriptor for your project and execute the following command:

MVN idea: idea
 

Use the goal command in combination. For example, package only without testing: MVN-Dtest package.
Content of the compilation test: MVN test-compile
Only jar package: MVN jar: jar
Test without compiling, or test compiling: MVN test-skipping compile-skipping test-compile (note the flexible use of-skipping here, of course, it can also be used for other combined commands)
Clear some system settings of Eclipse: MVN Eclipse: clean

Note:
After creating a project and executing MVN Eclipse: eclipse, the eclipse project file will be generated. However, after opening the project with eclipse for the first time, the following error is reported: unbound classpath variable: 'm2 _ repo/JUnit/3.8.1/junit-3.8.1.jar 'in project my-webapp, You need to configure the m2_repo variable in eclipse, configuration steps: window> preferences> JAVA> build path> classpath Variables
Create an m2_repo variable. The variable value points to the data warehouse location of maven2 in your system.

Add a resource file (. Resources)
How do I add resources to a jar file?

In the example application, add the directory $ {basedir}/src/main/resources to put the resource file in this directory. Any files and directories in the $ {basedir}/src/main/Resources Directory will be packaged into the jar file.

My-app

| -- Pom. xml

'-- SRC

| -- Main

| -- Java

| '-- Com

| '-- Mycompany

| '-- App

| '-- App. Java

| '-- Resources

| '-- META-INF

| '-- Application. Properties

'-- Test

'-- Java

'-- Com

'-- Mycompany

'-- App

'-- Apptest. Java
 

In this example, we put a META-INF directory under the $ {basedir}/src/main/Resources Directory and an application. properties file in the META-INF directory. If you decompress the JAR file, you can see the following:

| -- META-INF

| -- Manifest. MF

| -- Application. Properties

| '-- Maven

| '-- Com. mycompany. app

| '-- My-app

| -- Pom. Properties

| '-- Pom. xml

'-- Com

'-- Mycompany

'-- App

'-- App. Class
 

As you can see, the content in $ {basedir}/src/main/resources is at the beginning of the JAR file, and our application. properties file is under the META-INF directory. At the same time, some other files such as META-INF/manifest. MF, and Pom. xml and Pom. properties files are also in place, which is the standard behavior of Maven for jar files generated by Maven. You can create your own manifest, but if you do not specify it, MAVEN will generate a default one. Since both the Pom. xml and Pom. properties files are packaged into jar files, the products generated by Maven are self-described. A simple application is to get the application version. Some Maven tools may be required to open the POM file, but the standard Java API can be used to open the property file. The property file Pom. properties is as follows:

# Generated by Maven

# Tue Oct 04 15:43:21 GMT-05: 00 2005

Version = 1.0-Snapshot

Groupid = com. mycompany. app

Artifactid = My-app
 

For unit testing, you may need to add resources to classpath. You can follow the same pattern:

My-app

| -- Pom. xml

'-- SRC

| -- Main

| -- Java

| '-- Com

| '-- Mycompany

| '-- App

| '-- App. Java

| '-- Resources

| '-- META-INF

| -- Application. Properties

'-- Test

| -- Java

| '-- Com

| '-- Mycompany

| '-- App

| '-- Apptest. Java

'-- Resources

'-- Test. Properties
 

In the test code, you only need to use the following code to access these resources:

...

// Retrieve resource

Inputstream is = getclass (). getresourceasstream ("/test. properties ");

// Do something with the Resource

...

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/liu251/archive/2008/08/04/2767188.aspx

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.