Home maven
Post
Cancel

maven

commands

  • generating projects

    1
    2
    3
    4
    5
    
    mvn archetype:generate
      
    mvn archetype:generate "-DgroupId=com.fozu.helloworld" "-DartifactId=helloworld" "-Dversion=1.0-RELEASE" "-DarchetypeGroupId=org.apache.maven.archetypes" "-DarchetypeArtifactId=maven-archetype-quickstart"
      
    mvn archetype:generate "-DarchetypeGroupId=org.apache.maven.archetypes" "-DarchetypeArtifactId=maven-archetype-webapp" "-DarchetypeVersion=1.4"
    
  • compile

    1
    
    mvn compile
    
  • clean

    1
    
    mvn clean
    
  • test

    1
    
    mvn test
    
  • package

    1
    
    mvn package
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    <packaging>jar</packaging>
    ...
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib</classpathPrefix>
                    <mainClass>
                        com.fozu.Main
                    </mainClass>
                </manifest>
            </archive>
            <finalName>hello3</finalName>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.8</version>
        <executions>
            <execution>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>
                        ${project.build.directory}/lib
                    </outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2-beta-5</version>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>com.fozu.Main</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>
                    jar-with-dependencies
                </descriptorRef>
            </descriptorRefs>
            <finalName>hello3</finalName>
            <appendAssemblyId>false</appendAssemblyId>
        </configuration>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>3.0.1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>repackage</goal>
                </goals>
                <configuration>
                    <mainClass>com.fozu.Main</mainClass>
                    <finalName>hello3</finalName>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
    • install

      1
      2
      
      # 安裝jar到本地倉庫
      mvn install:install-file -Dfile=xxx.jar -DgroupId=com.fozu -DartifactId=xxx -Dversion=1.2.3 -Dpackaging=jar
      

阿里云maven仓库镜像配置

1
2
3
4
5
6
7
<mirror>
  <id>aliyun</id>
  <mirrorOf>central</mirrorOf>
  <name>aliyun</name>
  <url>https://maven.aliyun.com/repository/public</url>
  <blocked>true</blocked>
</mirror>

POM

jdk

1
2
3
4
5
6
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>

modelVersion

pom版本

1
<modelVersion>4.0.0</modelVersion>

groupId

1
<groupId>com.fozu.helloworld</groupId>

artifactId

1
<artifactId>02_helloworld</artifactId>

version

1
<version>1.0-RELEASE</version>
  • dependency.scope

    • compile

      在所有類路徑中均可用,且依賴會傳遞到其他相關項目

    • provided

      僅在編譯和測試路徑上可用,且不可傳遞,希望JDK或容器在運行時提供它

    • runtime

      依賴關係不是編譯所必需的,而是運行所必需的,在測試和運行路徑中可用,在編譯路徑中不可用

    • test

      依賴關係對於正常使用該應用程序不是必需的,僅在測試編譯和執行階段可用,且不可傳遞

    • system

      必須顯式提供jar的位置(可以通過systemPath標簽指定),不會去maven倉庫中查找

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      
      <dependency>
      	<gourpId>com.fozu</gourpId>
          <artifactId>helloworld</artifactId>
          <version>1.0.0</version>
          <scope>system</scope>
          <systemPath>${basedir}/lib/xxx.jar</systemPath>
      </dependency>
      
      <configuration>
      	<mainClass>com.fozu.Main</mainClass>
          <finalName>helleworld</finalName>
          <includeSystemCoope>true</includeSystemCoope>
      </configuration>
      

references

search.maven.org

mvnrepository.com

Default生命周期

  • validate
  • compile
  • test
  • package
  • verify
  • install
  • deploy
This post is licensed under CC BY 4.0 by the author.