Common commands of mvn

1. mvn help:describe Have you ever suffered because you can't remember which goals a plugin has, or have you been troubled because you can't remember which parameters a certain goal has, then try this command, it will tell you everything Parameter: 1. -Dplugin=pluginName 2. -Dgoal (or -Dmojo)=goalName: Used together with -Dplugin, it will list the goal information of a plugin. If it is not detailed enough, you can also add -Ddetail. (Note: a plugin goal is also considered a "Mojo") Let's run mvn help:describe -Dplugin=help -Dmojo=describe and feel it!

2. mvn archetype:generate How did you create your maven project? Is it like this: mvn archetype:create -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.ryanote -Dartifact=common if you still use it , then you are out, modern people use mvn archetype:generate, it will make the boring thing of creating a project more user-friendly, you no longer need to remember so many archetypeArtifactId, you only need to enter archetype:generate, the rest The next step is to do "choice questions".

3. mvn tomcat:run After using maven, you no longer need to use tomcat in eclipse to run web projects (in practice, it is often found that using it will cause asynchronous updates), just in the corresponding directory (such as /ryanote), run the mvn tomat:run command, and then run http://localhost:8080/ryanote in the browser to view it. If you want more customization, you can add the following configuration to the pom.xml file: 01 02 03 04 org.codehaus.mojo 05 tomcat-maven-plugin 06 07 /web 08 9090 09 10 11 12 Of course, you can also add parameters to the command to achieve specific functions, the following are more commonly used: 1. Skip tests :-Dmaven.test.skip(=true) 2. Specify the port:-Dmaven.tomcat.port=9090 3. Ignore test failure:-Dmaven.test.failure.ignore=true Of course, if your other related projects have To update, be sure to run mvn clean install in the project root directory to perform the update, and then run mvn tomcat:run to make the changes take effect.

4. mvnDebug tomcat:run This command is mainly used for remote testing. It will monitor port 8000 for remote testing. After opening remote testing in eclipse, it will run, set breakpoints, and debug. Everything is like this Simple. The parameters mentioned above also apply here.

5. mvn dependency:sources As the name implies, with it, you don't have to look for the source code everywhere, just run it, and the source code of the jar package that your project depends on will be there

 

Maven common commands

  • mvn archetype:create : create a Maven project

  • mvn compile : compile source code

  • mvn test-compile : compile test code

  • mvn test : run unit tests in the application

  • mvn site : a website that generates project-related information

  • mvn clean : clean the build results in the target directory

  • mvn package : Generate jar files according to the project

  • mvn install : install the jar in the local Repository

  • mvn eclipse:eclipse : Generate Eclipse project files

  • mvn -Dmaven.test.skip=true : ignore test documentation compilation
  • Maven common commands

    1. Detect Maven, JDK version mvn -v or mvn -version
    2. get help option mvn -h or mvn -help
    3. Display detailed error information mvn -e
    4. 创建Java项目 mvn archetype:create -DgroupId=${groupId} -DartifactId=${artifactId} 示例:mvn archetype:create -DgroupId=com.howsun -DartifactId=myApp-Dversion=0.1
    5. 创建Web项目 mvn archetype:create -DgroupId=${packageName} -DartifactId=${webappName} -DarchetypeArtifactId=maven-archetype-webapp
    6. Create other projects (such as SSH, JPA, JSF, Seam...) mvn archetype:generate and then select the project skeleton, groupid, artifactid, version number according to the prompts...Maven3 has hundreds of project skeletons
    7. Convert to Eclipse project mvn eclipse:eclipse mvn eclipse:clean //Clear the Eclipse setting information and convert it to an idea project: mvn idea:ide
    8. compile mvn compile
    9. . Compile test code mvn test-compile
    10. . Generate Site: mvn site
    11. .Test mvn test //Run the test mvn test -Dtest=${class name} //Run the test class alone
    12. . Clear mvn clean //will clear the original compiled result
    13. . Package mvn packagemvn package –Dmaven.test.skip=true //Do not execute tests when packaging
    14. . Publish mvn install //Package the project into a component and install it in the local warehouse mvn deploy //Publish to the local warehouse or server (such as Tomcat, Jboss)
    15. . Manually add artifacts to the repository mvn install:install-file -Dfile=${jar package file location} -DgroupId=${groupId} -DartifactId=${artifactId} -Dversion=${version number} -Dpackaging=jar -DgeneratePom =${Whether to create pom file at the same time}
    16. . Copy the dependencies to the corresponding directory mvn dependency:copy-dependencies -DoutputDirectory=${target directory} -DexcludeScope=${scope} -Dsilent=true Example: mvn dependency:copy-dependencies -DoutputDirectory=WebRoot/WEB-INF/lib -Dsilent=true -DincludeScope=runtime
    17. . Show details about a plugin (configuration, goals, etc.): mvn help:describe -Dplugin=pluginName –Ddetail
  • Project configuration file pom.xml

    Is the core configuration file of the Maven project, located in the root directory of each project, and indicates the metadata file of Maven's work. Node introduction

    1. : The root node of the file .
    2. : Object model version used by pom.xml.
    3. : The unique Id of the organization or group that created the project.
    4. : The unique Id of the project, which can be regarded as the project name.
    5. : Packaging type, generally JAR, WAR, EAR, etc.
    6. : The version number of the product.
    7. : The display name of the project, often used for documentation generated by Maven.
    8. : The organization's site, often used for Maven-generated documentation.
    9. : Description of the project, often used for documentation generated by Maven.
    10. . : Component Dependency: Model Inheritance
    11. . : dependency management
    12. . : create a report
    13. . :Construct
    14. . : refer to a third-party repository
    15. . : permission

     

     

    When we generate a maven project structure through a template (such as the simplest maven-archetype-quikstart plugin), how to convert it into a java project supported by eclipse?

    1. Navigate to the maven root directory (there must be pom.xml in this directory).

    2. Use the maven command mvn eclipse:eclipse

    3. Go to the root directory, you will find two familiar files are automatically generated: .classpath and .project.

    4. Open eclipse, find the project path, and import it.

     

    3.2. POM

    The full name of POM is Project Object Model, which is the project object model. pom.xml is the project description file of maven, which is similar to the project.xml file of antx. The pom.xml file describes the information of the project in the form of xml, including project name, version, project id, project dependencies, compilation environment, continuous integration, project team, contribution management, report generation, etc. In short, it contains all the project information.

    3.2.1. Basic configuration of pom.xml

    <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>org.codehaus.mojo</groupId>
      <artifactId>my-project</artifactId>
      <version>1.0</version>
    </project>
    

    modelVersion Describes which version of the project descriptor this POM file conforms to.

    groupId A universally unique identifier for an item. Usually a fully correct package name is used to distinguish it from similar names in other projects (eg: org.apache.maven).

    artifactId The identifier specified for the artifact is unique within the group given the groupID, and the artifact represents the component (product) that is made or applied by a project.

    version The version of the artifact generated by the current project

    The above four elements are indispensable, among which groupId, artifactId, version describe the unique identifier of the dependent project.

    3.2.2. pom.xml file structure

    <project>
      <modelVersion>4.0.0</modelVersion>
      <!- ​​Basic information about The Basics project->
      <groupId>...</groupId>
      <artifactId>...</artifactId>
      <version>...</version>
      <packaging>...</packaging>
      <dependencies>...</dependencies>
      <parent>...</parent>
      <dependencyManagement>...</dependencyManagement>
      <modules>...</modules>
      <properties>...</properties>
      <!- ​​Build Settings project's build settings->
      <build>...</build>
      <reporting>...</reporting>
      <!- ​​More Project Information Other project information->
      <name>...</name>
      <description>...</description>
      <url>...</url>
      <inceptionYear>...</inceptionYear>
      <licenses>...</licenses>
      <organization>...</organization>
      <developers>...</developers>
      <contributors>...</contributors>
      <!-- Environment Settings  ->
      <issueManagement>...</issueManagement>
      <ciManagement>...</ciManagement>
      <mailingLists>...</mailingLists>
      <scm>...</scm>
      <prerequisites>...</prerequisites>
      <repositories>...</repositories>
      <pluginRepositories>...</pluginRepositories>
      <distributionManagement>...</distributionManagement>
      <profiles>...</profiles>
    </project>
    

    project is the root node of pom.xml. For other elements, please refer to POM Reference

    3.2.3. 3 important relationships of POM

    POM has three very important relationships: dependency, inheritance, and composition.

    3.2.3.1. Dependencies

    <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.0</version>
          <type>jar</type>
          <scope>test</scope>
          <optional>true</optional>
        </dependency>
        ...
    </dependencies>

    If you want to depend on a jar package that is not in the maven library, the method is very simple, that is, first install the jar package into the local maven library using the following command:

    mvn install:install-file -Dfile=my.jar -DgroupId=mygroup -DartifactId=myartifactId -Dversion=1
    

    Then add the dependencies to it.

    3.2.3.2. Inheritance

    Another powerful change that maven brings is project inheritance.

    3.2.3.2.1. Defining the parent project
    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mygroup </groupId>
      <artifactId>my-parent</artifactId>
      <version>2.0</version>
      <packaging>pom</packaging>
    </project>

    The packaging type, defined as pom is used to define as parent and to synthesize multiple projects. Of course, the pom of the maven project we created all inherit the super pom of maven. If you want to see the complete pom structure of the project (parent or child), you can run:

    mvn help:effective-pom
    

    That's it.

    3.2.3.2.2. Subproject Configuration
    <project>
       <modelVersion>4.0.0</modelVersion>
       <groupId>com.mygroup </groupId>
       <artifactId>my-child-project</artifactId>
       <parent>
            <groupId>com.mygroup </groupId>
            <artifactId>my-parent</artifactId>
            <version>2.0</version>
            <relativePath>../my-parent</relativePath>
        </parent>
    </project>
    

    relativePath may not be needed, but it is used to specify the parent's directory for quick query.

    3.2.3.3. Composition Relations

    A project has multiple modules, also called multiple modules, or composite projects. Definition as follows:

    <project>
       <modelVersion>4.0.0</modelVersion>
       <groupId>com.mygroup </groupId>
       <artifactId>my-parent</artifactId>
       <version>2.0</version>
       <modules>
           <module>my-child-project1<module>
           <module>my-child-project2<module>
       </modules>
    </project>
    

    Where module describes the relative path of the subproject.

    3.2.4. dependencyManagement和Profile

            Maven also provides a dependencyManagement element that provides a way to unify dependency version numbers. The dependencyManagement element is generally used in the top-level parent POM. Using the dependencyManagement element in pom.xml allows you to reference a dependency in a subproject without explicitly listing the version number. Maven will go up the parent-child hierarchy until it finds a project with a dependencyManagement element, and then it will use the version number specified in the dependencyManagement element, which solves the problem of incompletely modifying the dependency version number.

            Maven's Profile element can customize a particular build for a particular environment, making build portability between different environments possible. For example, to run mvn install using the production profile, you need to pass the -Pproduction parameter on the command line, where production is the id of the profile. To verify that the production profile overrides the default Compiler plugin configuration, run Maven with debug input (-X) turned on like this.

     

    The Maven command to install the JAR package is:

    mvn install:install-file -Dfile=jar package location -DgroupId=groupId above -DartifactId=artifactId above -Dversion=version above -Dpackaging=jar


    For example:
    the jar package I downloaded was placed in the D:\mvn directory (D:\mvn\spring-context-support-3.1.0.RELEASE.jar),
    then the command I typed in cmd should be:

    mvn install:install-file -Dfile=D:\mvn\spring-context-support-3.1.0.RELEASE.jar -DgroupId=org.springframework -DartifactId=spring-context-support -Dversion=3.1.0.RELEASE - Dpackaging=jar and press

    Enter, it shows that the installation is successful:

     

    Maven: Install Jar to local repository and upload Jar to private server

     

    Example

    1. The dependencies are as follows:

    <dependency>
          <groupId>org.quartz-scheduler.internal</groupId>
          <artifactId>quartz-terracotta-bootstrap</artifactId>
          <version>2.2.2-SNAPSHOT</version>
    </dependency>

     

     

    Install the Jar package to the local repository command:

    mvn install:install-file -Dfile=D:\quartz-terracotta-bootstrap-2.2.2-SNAPSHOT.jar -DgroupId=org.quartz-scheduler.internal -DartifactId=quartz-terracotta-bootstrap -Dversion=2.2.2-SNAPSHOT -Dpackaging=jar

     

    -- DgroupId and DartifactId constitute the coordinates of the jar package in pom.xml, corresponding to the dependent DgroupId and DartifactId
    -- Dfile represents the absolute path of the jar package to be uploaded
    -- Dpackaging is the type of installation file

     

     

    2. Upload Jar to private server  

    Order:

    mvn deploy:deploy-file -DgroupId=org.terracotta.toolkit -DartifactId=terracotta-toolkit-api-internal -Dversion=1.12 -Dpackaging=jar -Dfile=D:\terracotta-toolkit-api-internal-1.12.jar -Durl=http://ip:port/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty

     

     

    -- DgroupId and DartifactId constitute the coordinates of the jar package in pom.xml, corresponding to the dependent DgroupId and DartifactId
    -- Dfile represents the absolute path of the jar package to be uploaded
    -- the exact URL address of the warehouse on Durl private server (open the left side of nexus The repositories menu, you can see the path)
    -- the DrepositoryId server's representation id, which can be seen in the nexus configuration

     

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326759409&siteId=291194637