Maven基础:常用技巧总结(1)

这篇文章总结一下Maven在使用中的常用的基础知识技巧。

1: GAV:Maven坐标

  • 格式示例
    <groupId>Maven坐标之:G</groupId>
    <artifactId>Maven坐标之:A</artifactId>
    <packaging>打包方式</packaging>
    <version>Maven坐标之:V</version>

作用:定位Maven工程。

2: parent的使用

  • 格式示例
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath/>
    </parent>

作用:常用于在多个项目中需要进行管理的公共依赖部分,在parent项目的pom文件中定义公共依赖,在子项目中通过<parent> </parent>引入

2: properties的使用

  • 格式示例
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

作用:通过properties可以设定或者修改Maven属性,在使用的时候通过${属性名称}的方式进行引用。

4: modules的使用

  • 格式示例
    <modules>
        <module>子模块A</module>
        <module>子模块B</module>
        <module>子模块C</module>
        <module>子模块D</module>
        <module>子模块E</module>
    </modules>

作用:通过modules可以进行Maven的多模块管理,各个子模块都用自己特定的pom文件,在父模块中通过modules引入子模块,在子模块中通过<parent> </parent>建立与父模块的关联。

5: dependencies的使用

  • 格式示例
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

作用:dependencies用于加载相应的依赖

6: build/plugin的使用

  • 格式示例
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

作用:build/plugin用于引入各种插件的功能,比如上述示例就是为了在Spring Boot中增加maven的编译插件(spring-boot-maven-plugin),如果pom继承了spring-boot-starter-parent的话,也可以不必明示地使用上述配置进行引入,因为spring-boot-starter-parent中已经包含。

7: build/resource的使用

  • 格式示例
    <build>
        <resources>
             <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.txt</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
    </build>

作用:如果没有特殊的设定,Maven构建时会按照标准的查找和处理各类型文件。比如源码目录下的mybatis的xml文件就不会被打包,而通过设定resource则可以按照需要进行设定。

8: exclusions的使用

  • 格式示例
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

作用:Maven的依赖会自动关联所有的jar,但是难免会出现版本不一致的情况,exclusion可以排除指定的jar文件。

9: maven-assembly-plugin的使用

  • 使用示例
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <finalName>filename-demo</finalName>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

使用到的assembly.xml示例如下所示:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"  
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">  
    <id>distribution</id>  
    <formats>  
        <format>tar</format>  
    </formats>  
  
    <fileSets>
        <fileSet>  
            <directory>target</directory>  
            <outputDirectory>lib</outputDirectory>  
            <includes>  
                <include>*.jar</include>  
            </includes>  
            <excludes>  
                <exclude>*keyword.jar</exclude>  
            </excludes>  
        </fileSet>  
    </fileSets>  
</assembly>

作用:按照指定的文件和排除的文件信息创建一个最后结果形式为tar的文件。

发布了1082 篇原创文章 · 获赞 1294 · 访问量 402万+

猜你喜欢

转载自blog.csdn.net/liumiaocn/article/details/104352426