Commands related to Maven compilation code

Source: Internet
Author: User
Document directory
  • Compile source code: MVN compile
  • Eclipse project: MVN Eclipse: Eclipse
  • Create an idea project: MVN idea: idea
  • Use the goal command in combination. For example, package only without testing: MVN-Dtest package.
  • Only jar package: MVN jar: jar

1. Compile the main code in the main directory using Maven and run the command in the root directory of the project.MVN clean compileInto

Compile the line project. 2. After the test cases under the test directory are compiled, you can call Maven to run the test. MVN clean testTest. Third, after the project is compiled and tested, the next important step is package ). Define the <packaging> war </packaging> label in POM. XML to indicate the type of package that the project will be packaged into (the default type is jar). We can simply execute the command MVN clean package. 4. The war target of the war plug-in packs the project master code into a definition in the <artifactid> s3h3webws </artifactid> label-<version> 0.0.1-Snapshot </version> label. war file (the final JAR file is a S3h3WebWs-0.0.1-SNAPSHOT.war), which is also located in the target/output directory, which is named according to artifact-version.jar rules, if needed, we can also use finalname to customize the name of the file. Fifth, now we get the project output. If we create a project as a jar file and need to use it in other projects, you can copy the JAR file to the classpath of other projects to use the classes in this project. However, how can we make other Maven projects directly reference this jar? We also need an installation step to execute MVN clean installCommand. Sixth, we have already experienced the most important Maven commands: MVN clean compile, MVN clean test, MVN clean package, MVN clean install. Compile is executed before the test is executed, and test is executed before the package is executed. Similarly, the package is executed before the installation. 7. If a class with the main method in the project needs to be called and executed. By default, the generated jar package cannot directly run the main method, because the class information with the main method is not added to manifest (we can open the META-INF/manifest in the jar file. mf file, you will not be able to see a line of main-class ). To generate executable jar files, we need to use the maven-shade-plugin plug-in to configure the plug-in as follows: <plugin> <groupid> Org. apache. maven. plugins </groupid> <artifactid> Maven-shade-plugin </artifactid> <version> 1.2.1 </version> <executions> <execution> <phase> package </phase> <goals> <goal> shade </goal> </goals> <configuration> <transformers> <transformer implementation = "org. apache. maven. plugins. shade. resource. manifestresourcetransformer "> <mainclass> com. Todaytech. framework. utils. voconvertxmlutils </mainclass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> the relative position of the plug-in element in the POM should be in the <project> <build> <plugins>. We have configured mainclass as com. todaytech. Framework. utils. voconvertxmlutils. The project will put this information in manifest during packaging. Currently running MVN clean installAfter the build is complete, open the target/directory, we can see the S3h3WebWs-0.0.1-SNAPSHOT.jar and original-S3h3WebWs-0.0.1-SNAPSHOT.jar, the former is a runable jar with main-class information, the latter is the original jar, open the hello-world-1.0-SNAPSHOT.jar/manifest for the META-INF. mf, you can see that it contains such a line of information: Main-class: COM. todaytech. framework. utils. voconvertxmlutils now, we execute this jar file in the project root directory: D: \ code \ s3h3webws> JAVA-jar target \ S3h3WebWs-0.0.1-SNAPSHOT.jar Hello Maven console output as Hello Maven, this is exactly what we expected. 8. Use Maven archetype to create the skeleton of the project and exit the current Maven project directory. For Maven 3, run MVN archetype: Generate. For Maven 2, run the following command: MVN Org. apache. maven. plugins: Maven-Archetype-plugin: 2.0-alpha-5: generate many documents will allow you to directly use the simpler MVN archetype: Generate command, but this is not safe in maven2, because this command does not specify the version of the archetype plug-in, Maven automatically downloads the latest version, which may lead to an unstable snapshot version, resulting in operation failure. However, in Maven 3, Maven only resolves the latest stable version even if the version is not specified. Therefore, this is safe. We are actually running the plug-in Maven-Archetype-plugin. Note the colon separation. The format is groupid: artifactid: Version: Goal, org. apache. maven. plugins is the groupid of the Maven official plug-in, and Maven-archetype in is the artifactid of the archetype plug-in. 2.0-alpha-5 is the latest stable version of this plug-in, generate is the target plug-in. Then we will see a long piece of output, there are a lot of available archetype for us to choose from, including the archetype of the famous appfuse project, the archetype of the JPA project, and so on. Each archetype corresponds to a number, and the command line prompts a default number. The corresponding Archetype is Maven-Archetype-Quickstart. Press enter to select this archetype, then Maven prompts us to enter the groupid, artifactid, version, and package name of the project to be created. Bytes --------------------------------------------------------------------------------------------------

Generate and clear the Eclipse project structure:
MVN Eclipse: Eclipse
MVN Eclipse: clean

Clear (delete the compiled content in the target directory ):
MVN clean

Only package web page files:
MVN war: exploded

Skip the test during packaging:
MVN package-dmaven. Test. Skip = ture

Skip the test to run the maven task:

MVN-dmaven. Test. Skip = true xxx

 

Create a normal Java project for Maven (only applicable to maven2.x ):
MVN archetype: Create-dgroupid = packagename-dartifactid = projectname: Create a Maven WEB Project (applicable only to maven2.x): MVN archetype: create-dgroupid = packagename-dartifactid = webappname darchetypeartifactid = Maven-Archetype-webapp create a Maven WEB Project (only applicable to maven3.x): C: \ Documents and Settings \ Administrator> F: f: \> Cd F: \ Software \ Maven \ mavenwebapptestf: \ Software \ Maven \ mavenwebapptest> MVN archetype: generate-dgroupid = com. shihuan-dartifactid = s3h3webws-darchetypeartifactid = Maven-Archetype-webapp-dinteractivemode = false create a Maven Quickstart project (applicable only to maven3.x): C: \ Documents ents settings and \ Administrator> F: f: \> Cd F: \ Software \ Maven \ mavenwebapptestf: \ Software \ Maven \ mavenwebapptest> MVN archetype: generate-dgroupid = com. shihuan-dartifactid = s3h3webws-darchetypeartifactid = Maven-Archetype-Quickstart-dinteractivemode = false run the command: MVN archetype: Create-dgroupid = com. shihuan-dartifactid = s3h3webws-e-darchetypeartifactid = Maven-Archetype-webapp or MVN archetype: Create-dgroupid = com. shihuan-dartifactid = s3h3webws-X-darchetypeartifactid = Maven-Archetype-webapp source code: MVN compile test code: MVN test-compile run test: MVN test generate site directory: MVN site to generate the site directory and publish: MVN site-deploy install the output file of the current project to the local repository: MVN install the specified file to the local repository: MVN install: install-file-dgroupid = <groupid>-dartifactid = <artifactid>-dversion = 1.0.0-dpackaging = jar-dfile = <myfile. jar> View the actual pom information: MVN help: Valid tive-Pom The dependency information of the analysis project: MVN dependency: analyze or MVN dependency: tree. view the help information: MVN help: help or MVN help: help-ddetail = true view the help information of the plug-in: MVN <plug-in>: help, for example, MVN dependency: help or MVN ant: help to generate Eclipse project: MVN Eclipse: eclipse generates an idea project: MVN idea: idea and uses the goal command in combination. For example, if you only package but not test: MVN-Dtest package, only the jar package: MVN jar: jar is used for testing and not compiling, also do not test the compilation: MVN test-skipping compile-skipping test-compile

Add the servlet container plug-in to the Pom. xml file:

<build>   <plugins>       <plugin>           <groupId>org.codehaus.mojo</groupId>           <artifactId>tomcat-maven-plugin</artifactId>       </plugin>       <plugin>           <groupId>org.mortbay.jetty</groupId>           <artifactId>maven-jetty-plugin</artifactId>           <version>6.1.6</version>       </plugin>       <plugin>           <artifactId>maven-compiler-plugin</artifactId>           <configuration>               <source>1.6</source>               <target>1.6</target>               <encoding>UTF-8</encoding>           </configuration>        </plugin>    </plugins></build>

 

 

 

Start Tomcat:

MVN Tomcat: Run

 

Start jetty:

MVN jetty: Run

 

 

Convert to eclipse project:

MVN-dwtpversion = 1.5 Eclipse: Eclipse

 

In this way, the WTP plug-in web project is generated.

Open eclipse and choose File> Import> General> existing projects into workspace from the menu. In the dialog box, select a directory and import it.

In addition, you must create a classpath variable named m2_repo in eclipse and set the value to the. m2/Repository directory of the System user.

 

Packaging: The packaging format can be POM, jar, Maven-plugin, EJB, war, ear, rar, and Par.

 

 

Relationship among pom: dependencies, parent, and modules ). Example of dependency:
<dependency>    <groupId>org.hibernate</groupId>    <artifactId>hibernate</artifactId>    <version>3.2.6.ga</version></dependency>

Examples of inheritance relationships:

Inherit from other Pom. xml configurations.
Maven provides a top-level parent Pom. xml file similar to Java. Lang. object.
You can run the following command to check whether the current Pom. xml file is affected by the Pom. xml file:MVN help: proactive-POM.

Create a reusable Pom. xml file for various projects: http://easymorse.googlecode.com/svn/trunk/pom/pom.xml
Deploy the Pom. xml file to be reused:MVN install.
Inherit the above POM in your pom file:
<parent>    <groupId>com.easymorse</groupId>    <artifactId>pom</artifactId>    <version>0.1</version></parent>

Examples of aggregation relationships:
It is used to aggregate multiple Maven projects into a large project. For example, the directory structure is as follows :. | -- pom. XML | -- module-a' -- pom. XML | -- module-B '-- pom. XML | -- module-C' -- pom. XML | -- foo-all' -- pom. XML, the total pom. the XML file is similar :... <modules> <module> module-A </module> <module> module-B </module> <module> module-C </module> <module> foo-all </module> </modules> </Project>
 
Deploy the project to Tomcat:
 
Tomcat configuration administrator: conf \ tomcat-users.xml.
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="manager"/>
  <user username="marshal" password="password" roles="manager"/>
</tomcat-users>
 
Add the following to the Tomcat plug-in of the POM file:
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>tomcat-maven-plugin</artifactId>
    <configuration>
        <url>http://localhost:8080/manager</url>
        <server>myserver</server>
        <path>/mycontext</path>
    </configuration>
</plugin>
 
Add the following in the. m2/settings. xml file:
<settings 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/settings-1.0.0.xsd">
        <servers>
            <server>
                <id>myserver</id>
                <username>marshal</username>
                <password>password</password>
            </server>
        </servers>
</settings>
 
Run the package deployment, which is in the maven project directory:
mvn tomcat:deploy
 
Then access: http: // localhost: 8080/mycontext.
Undeploy:
mvn tomcat:undeploy
 
Start a web application:
mvn tomcat:start
 
Stop a web application:
mvn tomcat:stop
 
Redeployment:
mvn tomcat:redeploy
 
Deploy the expanded war file:
mvn war:exploded tomcat:exploded
 
--------------------------------------------------------------------------------------

Pom refers to the project object model. Pom is an XML file, which is Pom. XML in maven2. It is the basis of Maven's work. When executing a task or goal, Maven reads Pom. XML from the root directory of the project to obtain the required configuration information.
The pom file containsProject information and configuration information required for the maven build projectUsually there are project information (such as version, member), Project dependencies, plug-ins, goal, build options, and so on.
POM can be inherited.Generally, for a large project or multiple modules, the POM of the sub-module must specify the POM of the parent module.
The top-level element of the project pom file.
The object model version used by modelversion is mandatory to ensure stable use. You do not need to modify the template unless the maven developer upgrades the template.
Groupid is the only identifier for creating a group or organization for a project. It is usually a domain name written down. For example, groupid org. Apache. Maven. plugins is reserved for all Maven plug-ins.
Artifactid is the unique base address name of the project artifact.
Packaging artifact packaging methods, such as jar, war, and ear. The default value is jar. This not only indicates the final Suffix of the project, but also indicates what kind of <a href = "http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Built-in_Lifecycle_Bindings" target = "_ blank"> lifecycle </a> is used in the build process.
The version of artifact is usually 0.0.1-snapshot, in which snapshot indicates that the project is under development and is the development version.
Name indicates the project display name, which is used in Maven-generated documents.
URL indicates the Project address, which is used in the document generated by Maven.
Description indicates the project description, which is used in the document generated by Maven.
Dependencies indicates dependency. Add the groupid artifactid and version of the dependency to the subnode dependencies.
Build indicates build configuration.
Parent indicates the parent pom.
WhereGroupid: artifactid: Version uniquely identifies an artifact.



Artifact
This is a bit difficult to explain. It is generally the file to be generated by a project, which can be a jar file, source file, binary file, war file, or even a pom file. Each artifact is composedGroupid: artifactid: VersionUniquely identifies an identifier. Artifact to be used (dependent) must be placed in the repository (see repository.



Repositories
Repositories is used to store artifact. If artifact generated by our project is a small tool, repositories is a repository with our own tools and other artificial tools, when a tool is required in the project, the dependency is declared in the Pom. When the code is compiled, the artifact)For your own use.
After your project is completed, you can run the MVN install command to put the project in the repository (repositories ).
Warehouses are divided into local warehouses and remote warehouses. Remote warehouses refer to warehouses used to store artifact on remote servers, and local warehouses refer to warehouses that store artifact locally, for Windows machines, the local repository address is system user. m2/Repository.



Build Lifecycle
A Project Build Process. Maven build lifecycle can be divided into three types:Default(Deploy the processing project ),Clean(Process project cleanup ),Site(Process project document generation ). They all contain different lifecycle.
Build lifecycle is composedPhasesThe following describes the important phase of default build lifecycle:
Verify that the project is correct and the required information is available;
Compile the source code of compile;
Test the compiled code, that is, execute the unit test code;
Package the compiled package code and generate the package file in the target directory;
Integration-test processes the package so that it can be deployed to the integrated test environment as needed;
Verify checks whether the package is valid and meets quality standards;
Install the package to the local repository to facilitate use of other local projects;
Deploy deployment, copy the final package to the remote warehouse and develop for him. This or project is shared and completed in the integration or release environment..

The above phase is ordered (note that other phase files are omitted between two adjacent phase files. For details about the complete phase, seeLifecycle), The execution of the following phase must be completed after the previous phase.
If you directly use a phase as goal, the phase before it is executed, such as MVN install
Validate, compile, test, package, integration-test, verify, and then install phase.


Goal
Goal represents a specific task
A goal represents a specific task (finer than a build phase) which contributes to the building and management of a project.

For example
MVN package indicates the Package task. We know from the above introduction that the execution of this task will first execute the phase before package phase.
MVN deploy indicates the deployed task
Mven clean install indicates that the clean phase (including other sub-phase) is executed first, and then the install phase is executed.

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.