The first Maven project (Linux) Maven's study notes

  Maven is a project management tool Apache's Java project management jar package, with it you can easily build and manage our Java project, you do not have to individually look for third-party jar package you need on the Internet, you simply maven repository ( https://mvnrepository.com/ search on it on), including all versions. The following learning process record first Maven project, before learning need to configure the JDK and Maven environment variables need to find specific data structures, is not difficult. Note: JDK version 1.7 and above need, Maven's official website there are instructions to download (requirement). If you enter the terminal command mvn -v displays the following information for the installation was successful, otherwise the installation fails.

  Hirofumi content:

  1. Create a maven project files
  2. Description maven project files
  3. Preparation of project source code and test code
  4. Compile the project, run the test, and package
  5. Site and generate a report file

  1. Create a maven project files

  Create a maven project file, execute mvn archetype in the terminal: generate it, execute the command

mvn archetype:generate

  It will automatically download some things need to plug-in file, and then select the required type of project (Choose archetype), I press enter to select the default (default is 7: internal -> org.apache.maven.archetypes: maven-archetype-quickstart ( An archetype which contains a sample Maven project.)), contain maven engineering sample.

  Then to enter groupId, artifactId, version and package, groupId generally enter the company name on it, and artifactId enter a project name, package name for the package. As shown below:

  Finally, enter confirms, prompts to create success.

  Execute change directory command, enter the project created to view the project file structure is created.

cd firstmaven # Change directory to firstmaven 
Tree # view the folder directory structure

  2. maven project files Description

  The above block diagram can see the structure maven project by the src directory a pom.xml file and the composition of, and src divided into the following main test. maven pom.xml file is the project management file, you can add dependency (ie jar package), set compilation, packaging and other plug-ins to the file. main folder to store the project's source code, test code to test the files are stored (by default created here helloworld of the App template).

  • pom.xml file (automatically generated)
<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>edu.gzmtu</groupId>
  <artifactId>firstmaven</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <name>firstmaven</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>
  </dependencies>
</project>

  The above pom.xml file is generated by default, which is described in detail below:

<groupId>: specify when you create the project groupId
<artifactId>: specify when you create the project artifactId
<Version>: specify when you create a project Version
<Packaging>: After compiling a good project, labeled jar package installation release
<denpendency>: test when needed JUnit jar package, groupId as junit, artifactId as junit, version is 3.8.1, scope for the test.

  • main folder

   App.java main project folder to store java source files, such as creating a template file

>>main/java/lollipop/App.java

package lollipop;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}
  • test folder

  AppTest.java test source code files stored test documentation, such as template files created

>>test/java/lollipop/AppTest.java

package lollipop;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

  3. Write project source code and test code

  Write a string here a statistical number of occurrences of a character Util class, and write test methods UtilTest class. Util class which is stored in the main directory of lollipop packages, UtilTest class storage lollipop package in the test directory, and modify the junit version 4.x version, 3.x version of the test being given to me.

>>main/java/lollipop/Util.java

Lollipop Package Penalty for; 

/ ** 
 * a statistical tool class characters 
 * @author Super Lollipop 
 * @version 2019/10/6 
 * / 
public class Util Final { 
    / ** 
     * Statistical functions in a string of occurrences of a character 
     * @param string a string 
     * @param character of a string 
     * @return int digital 
    * * / 
    public static int countCharacter (string string, char Character) { 
        char [] = string.toCharArray chars (); 
        int COUNT = 0 ; 
        for (C char: chars) { 
            IF (C == Character) { 
                COUNT = COUNT +. 1; 
            } 
        } 
        return COUNT; 
    } 
}

>>test/java/lollipop/UtilTest.java

Lollipop Package; 

Import junit.framework.Assert; 
Import org.junit.Test; 

/ ** 
 * test tools test class Util 
 * Super @author Lollipop 
 * @version 2019/10/6 
 * / 
public class UtilTest { 

    / ** 
     * unit test, to test for countCharacter Util * / 
    @Test 
    public void testCountCharacter () { 
        String String = "meter-Lollipop section 164-09"; 
        char Character = '-'; 
        int countExpect = 2; 
        int cOUNT = Util. countCharacter (String, Character); 
        Assert.assertEquals (countExpect, COUNT); 
    } 
}

>> pom.xml file (junit revised version is 4.10)

<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>edu.gzmtu</groupId>
  <artifactId>firstmaven</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <name>firstmaven</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>4.10</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

  4. Compile the project, run the test, and package

  The project is compiled using command mvn compile, execute the command:

mvn compile

              ......

  You can see the success of the compilation (BUILD SUCCESS), enter mvn clean can be compiled class files removed. The following start the test, execute the command:

mvn test

           ......

   We can see the test run two classes, AppTest and UtilTest (template created) (I created) tests pass. We can also put this project packaged into a jar file, enter the command:

mvn install

       ......

   We can see /home/lollipop/.m2/repository/edu/gzmtu/firstmaven/1.0/firstmaven-1.0.jar,mvn our project files stored maven jar package management repository directory on the message. ~ / .M2 / repository the command to view the execution ll / edu / gzmtu / firstmaven / 1.0 / document, see the package file generated jar

  5. Generate site and report files

  Before performing the generation site and report files, you need to add plug-ins, specifically add maven-project-info-reports-plugin and maven-javadoc-plugin, only need to modify the pom.xml file can be added plugin.

>> pom.xml (add a plug-in)

<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>edu.gzmtu</groupId>
  <artifactId>firstmaven</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <name>firstmaven</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>2.7</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>2.7</version>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
  • Generate site files 

  Generate site file enter the command mvn site on it, it will generate an index.html file and stored in the target / site / directory, enter the command:

mvn site

        ......

  Open the site file browser, file contents display information of the project, including plug-ins, copyright, and other information team (I do not have to configure). As shown below:

  • Generate reports document

  Generate reports document is generated API Doc document, and the document JDK Doc similar, it generates an index.html file and stored in the target / site / apidocs / directory, enter the command:

mvn javadoc:javadoc

        ......

  Open the report file browser, the display file API Java source code for the project, as shown below:

Guess you like

Origin www.cnblogs.com/quanbisen/p/11628761.html