MAVEN (ii)--using Maven

Source: Internet
Author: User
Tags xmlns
using Maven 3.1 using MAVEN to build your own project

(1) switch to the directory where the project is located, such as: D:\\develop\\apps

(2) Execute the following command:

MVN archetype:generate So you can create a MAVEN project based on the prompts.

Can be followed by a lot of parameters, commonly used are:

-dgroupid=com.company.app Group ID

-dartifactid=app project name, MAVEN creates a new directory under the current directory named this name to build the project

Whether the-dinteractivemode=false is in interactive mode and, if False, establishes the project with default settings

The following is an example: CMD code MVN archetype:generate-dgroupid=com.tiantian-dartifactid=jxc-dinteractivemode=false

Running the above code will create a new MAVEN project called Jxc under D:\\develop\\apps.

The above is just a simple Java application, so what if I want to build a Web application? The following is an example: CMD code MVN archetype:generate-darchetypegroupid=org.apache.maven.archetypes-darchetypeartifactid= Maven-archetype-webapp-dgroupid=com.myhost.myapp-dartifactid=myapp

In fact, the parameter archetypeartifactid is omitted when using Maven to build a simple project, and Maven-archetype-quickstart is used by default when this parameter is not specified. So to write the instructions for a MAVEN project that builds a common Java application above should be: CMD code MVN archetype:generate–darchetypegroupid= Org.apache.maven.archetypes–darchetypeartifactid=maven-archetype-quickstart–dgroupid=com.tiantian–dartifactid= Jxc–dinteractivemode=false

From the above we can see what kind of project you need to use Archetype,archetype is a template for MAVEN-defined project types, a directory schema for the corresponding project. The archetype is specified by the parameter archetypeartifactid. In addition to the two types of archetype described above, MAVEN provides us with the following kinds of archetype.

Archetypeartifactid

Description

Maven-archetype-archetype

Contains an example of a archetype, mainly used when we want to build our own archetype.

Maven-archetype-j2ee-simple

Contains a simple example of a Java EE application

Maven-archetype-plugin

Contains an example of a MAVEN plugin

Maven-archetype-plugin-site

Contains an example of a MAVEN plugin site

Maven-archetype-portlet

An example that contains a portlet

Maven-archetype-simple

Contains a simple MAVEN project

Maven-archetype-site

Contains a Maven site example that shows some supported document types, including APT, Xdoc, and FML

Maven-archetype-site-simple

Contains an example of a MAVEN site

The contents of how to build your own archetype will be covered in a later section. 3.2 Using MVC compile for source code compilation

After the project JXC is established under directory D:\\develop\\apps, we can enter the directory D:\\DEVELOP\\APPS\\JXC execute instructions mvn compile to compile. The compiled file will be placed in the target directory under the project root directory.

3.3 Compiling test source using MVC test-compile

After the project JXC is established under directory D:\\develop\\apps, we can switch the directory to d:\\develop\\apps\\jxc in CMD and then execute the instruction MVC test-compile to compile the test source.

3.4 Using MVC test to compile the source code and test the source code for testing

Execute instructions under directory D:\\DEVELOP\\APPS\\JXC MVC test will compile the source code, compile the test source, and then execute the test.

3.5 Project Packaging using the MVN package

Execute instructions in the directory where the project is located MVN package can be packaged for projects, The packaging is defined in the packaging element in the Pom.xml file under the project root, and if defined as a jar, as a jar package, and if the war is the war package, if the ear is the ear packet. If the packaging element is not defined in Pom.xml, the default value jar is used. The packaged files will be placed under the target directory at the root of the project. The packaged file name is the form of the artifactid-version defined in the Pom.xml file, such as the Artifactid defined in Pom.xml, which is a hello, and the version defined is 1.0, then the names of the files generated after packaging will be hello-1.0.

3.6 Installing the jar package to Maven's local repository using MVN install

Using mvn install, the MVN package jar can be installed into MAVEN's local repository. The local warehouse is ~/.m2/repository by default and you can define the path to the local warehouse in the localrepository tag in the conf/settings file in the MAVEN installation directory.

    When we need to install a local jar package to the local repository we can use the "MVN install:install-file" directive, where several parameters need to be specified when using the directive. File: Indicates the path of the local jar package, GroupId: Represents the Groupid;artifactid after the jar package is installed in the local warehouse: Artifactid after the jar package is installed to the local warehouse ; version: Indicates the corresponding release after the installation to the local warehouse; Packging: Indicates the type of the current component being referenced, since we are installing the jar package then the corresponding nature is the jar. example, if we now need to install a jar package "C:\jars\abc.jar" to our Maven local repository, then we can execute the following command in the command window: cmd code    mvn install: install-file -dfile=c:\jars\abc.jar -dgroupid=com.tiantian -dartifactid=abc -dversion=1.0  -Dpackaging=jar  

This will take our "C:\jars\abc.jar" to GroupID as "Com.tiantian", Artifactid "abc", Version "1.0", packaging type jar to our local warehouse, We can then refer to the dependency type directly in our local MAVEN project. Example: XML code <dependency> <groupId>com.tiantian</groupId> <artifactid>abc</artifactid > <version>1.0</version> </dependency>

3.7 Using mvn deploy to install the current project to the remote repository 3.8 Use MVN clean to clear the target directory where temporary files are stored

3.9 Put the resource file in the jar package

If you need to package some resource files in the jar package, you need to create a new resources directory under ${basedir}/src/main, and then put all the resource files in this directory so that the files will be placed under the classpath. If you need to access the corresponding resources in the test code, then the relative need to create a new resources directory under ${basedir}/src/test, and then put the corresponding resource files in this directory.

  3.10        filtering resource Files

   sometimes some of the values in the resource file we need to specify dynamically at compile time, MAVEN allows us to specify the ${property_name} when we build the file, so that when we compile it will automatically put Property_ Replace ${property_name} with the value corresponding to name. This property can be a value defined in Pom.xml, or it can be a value defined in settings.xml, or it can be a value defined in an external subordinate file, or it can be a system attribute. MAVEN's ability to dynamically replace property values is off by default, and if you want to open it, you need to specify the value of filtering to true in the project's Pom.xml file, false by default. The following example: XML code    <project xmlns= "http://maven.apache.org/POM/4.0.0"  xmlns:xsi= "http:// Www.w3.org/2001/XMLSchema-instance "     xsi:schemalocation=" http://maven.apache.org/POM/ 4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd ">     <modelversion>4.0.0 </modelVersion>        <groupId>com.tiantian.mavenApp</groupId>      <artifactId>mavenApp</artifactId>     <version> 1.0-snapshot</version>     <packaging>jar</packaging>       &NBSP;&NBSP;&LT;NAME&GT;MAVENAPPTEST&LT;/NAME&GT;&NBSP;    <url>http://maven.apache.org</url>        < properties>      <project.build.sourceencoding>utf-8</ project.build.sourceencoding>     </properties>         <dependencies>      <dependency>         <groupId>junit</groupId>        <artifactid>junit </artifactId>        <version>3.8.1</version>        <scope>test</scope>      </dependency>      </dependencies>         <build>       <resources>        <resource>    &NBSP;&NBSP;&NBSP;&NBSP;&NBSp;   <directory>src/main/resources</directory>            <filtering>true</filtering>        </ resource>      </resources>     </build>   </project>  

From the above file we can see that filtering this parameter is specific to the resource file directory, and we can also use directory to specify the location of resource files, the default is Src/main/resources. This build element has a default value because we need to change the value of the filtering, so we need to redefine the build and then overwrite the default value inside.

3.10.1 using elements element in Pom.xml and settings.xml as attributes

We can use the value of element in the Pom.xml file as the property value. In Maven, a POM is used to represent the root element in Pom.xml project, so we can use ${pom.name} to represent the name element below the project element, using ${pom.version} To represent the version element below the project element. Some elements are not explicitly defined in the Pom.xml file, but that does not mean that they do not exist, because they have a default value, which we can also use directly, such as this element. The same time we need to use the elements in Settings.xml can be associated with the settings prefix, such as ${settings.localrepository} Is the value of the localrepository element defined in Settings.xml. The following is a set of examples where files are defined under the Resources directory:

File Application.properties Properties Code project_name=${pom.name} project_version=${pom.version} project_modelversion =${pom.modelversion} Project_artifactid=${pom.artifactid} settings_localrepository=${settings.localrepository}

File Test.txt txt code project_version=${pom.version} project_modelversion=${pom.modelversion} project_artifactid=${ Pom.artifactid} Settings_localrepository=${settings.localrepository}

After defining the above two files in the resources directory, enter the level directory of the Pom.xml file in the command line mode, which is the root directory of the project, then compile with mvn compile and compile to target/ Classes directory to find the corresponding resource file, open can see the following:

Contents of the Application.properties File: Properties code project_name=mavenapptest project_version=1.0-snapshot project_ modelversion=4.0.0 Project_artifactid=mavenapp Settings_localrepository=d:\\develop\\mavenrepository

Contents of the Test.txt file: TXT code project_version=1.0-snapshot project_modelversion=4.0.0 Project_artifactid=mavenapp Settings_ Localrepository=d:\\develop\\mavenrepository

If we only need to process the resource file, we can also use the MVN process-resources directive, which is dedicated to working with resource files, and MVN compile is processing the resource files at compile time. 3.10.2 using an external file's properties Association

       to use the properties of an external file to correlate the attribute values defined in the resource file, we need to tell Maven where to look for this property file, This is specified by specifying the value of the filter in Pom.xml. XML code    <project xmlns= "http://maven.apache.org/POM/4.0.0"  xmlns:xsi= "http://www.w3.org/ 2001/xmlschema-instance "     xsi:schemalocation=" http://maven.apache.org/pom/4.0.0  Http://maven.apache.org/xsd/maven-4.0.0.xsd ">     <modelversion>4.0.0</ modelversion>         <groupId>com.tiantian.mavenApp</groupId>      <artifactId>mavenApp</artifactId>     <version> 1.0-snapshot</version>     <packaging>jar</packaging>          <name>mavenAppTest</name>     <url>http:// maven.apache.org</url>         <properties>        <project.build.sourceencoding>utf-8</project.build.sourceencoding>      </properties>         <dependencies>        <dependency>         <groupid>junit</groupid >         <artifactId>junit</artifactId>         <version>3.8.1</version>          <scope>test</scope>       </dependency>     

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.