Maven pom

作者: 青丝如梦 | 来源:发表于2019-05-21 13:50 被阅读0次

    设置文件编码

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

    Pom标签

    <resources>(IDEA build(构造)时指定资源文件)

    指定的资源路径<directory>下的文件,会自动copy到 target/classes 目录下

        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                    <includes>
                        <include>mapper/**</include>
                    </includes>
                </resource>
                <resource>
                    <directory>src/main/resources/env/dev</directory>
                </resource>
            </resources>
        </build>
    

    <profiles> 打包时触发多环境配置

        <profiles>
            <profile>
                <id>dev</id>
                <properties>
                    <env>dev</env>
                </properties>
                <!--  设置package -P默认使用dev环境  -->
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
            </profile>
            <profile>
                <id>test</id>
                <properties>
                    <env>test</env>
                </properties>
            </profile>
        </profiles>
    

    <plugin>插件

    子项目使用父项目的插件:

            <!--父 在pluginManagement标签中定义-->
            <pluginManagement>
                <plugins>
                    <plugin>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <inherited>true</inherited>
                        ......
                    </plugin>
                </plugins>
            </pluginManagement>
    
                <!--子-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                </plugin>
    

    maven-compiler-plugin(编译插件)

        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF8</encoding>
                    </configuration>
                    <version>3.8.1</version>
                </plugin>
            </plugins>
        </build>
    
    

    maven-resources-plugin(负责在compile编译时,处理resources下资源文件到target下)

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.1.0</version>
                    <executions>
                        <execution>
                            <id>copy-resources</id>
                            <!-- here the phase you need -->
                            <phase>validate</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${basedir}/target/classes</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>src/main/resources/env/dev</directory>
                                        <filtering>true</filtering>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-resources-plugin</artifactId>
                        <version>3.0.2</version>
                        <configuration>
                            <outputDirectory>${project.build.directory}/conf</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                            <encoding>UTF-8</encoding>
                        </configuration>
                    </plugin>
    
    

    maven-surefire-plugin(Maven package跳过单元测试插件)

    1. 使用maven.test.skip,不但跳过单元测试的运行,也跳过测试代码的编译。
      mvn package -Dmaven.test.skip=true
    2. 使用插件:
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.2</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
    

    maven-jar-plugin(打包插件)

                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>com.idss.argus.Application</mainClass>
                                <addClasspath>true</addClasspath>
                            </manifest>
                            <manifestEntries>
                                <Class-Path>./</Class-Path>
                            </manifestEntries>
                        </archive>
    <!--                打包时排除resources下指定文件-->
                        <excludes>
                            <exclude>mapper/</exclude>
                            <exclude>*.properties</exclude>
                            <exclude>*.yml</exclude>
                            <exclude>*.xml</exclude>
                        </excludes>
                    </configuration>
                    <version>3.1.1</version>
                </plugin>
    

    maven-dependency-plugin(依赖处理插件)

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.8</version>
                    <executions>
                        <execution>
                            <id>copy-dependencies</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/package/lib</outputDirectory>
                            </configuration>
                        </execution>
                        <!--打包使用-->
                        <execution>
                            <id>unpack-dependencies</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>unpack-dependencies</goal>
                            </goals>
                            <configuration>
                                <includes>**/application-variable.yml</includes>
                                <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    

    maven-assembly-plugin (打包装配插件)

    package打包时包含pom中所有dependencies(注意使用maven-jar-plugin指定启动类)

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>3.1.1</version>
                    <!-- 防止插件被继承,默认为true-->
                    <inherited>false</inherited>
                    <configuration>
                        <!-- 只需要一个jar时不需要指定assembly配置文件 -->
                        <descriptors>
                            <descriptor>assembly/assembly.xml</descriptor>
                        </descriptors>
                        <!-- 指定生成文件的名称-->
                        <finalName>${project.artifactId}</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                        <archive>
                            <manifest>
                                <mainClass>com.idss.argus.Application</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <!-- 打包时包含所有依赖,包含所有依赖的jar包的后缀名 -->
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id> <!-- this is used for inheritance merges -->
                            <phase>package</phase> <!-- bind to the packaging phase -->
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    

    resources结构:

    image.png

    assembly.xml

    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
        <id>assembly</id>
        <formats>
            <!--输出格式-->
            <format>tar.gz</format>
        </formats>
        <includeBaseDirectory>true</includeBaseDirectory>
        <fileSets>
            <!-- 将 ${basedir}/src/main/bin(启动脚本)下的所有文件放入bin目录中 -->
            <fileSet>
                <directory>${basedir}/src/main/bin</directory>
                <outputDirectory>bin</outputDirectory>
                <fileMode>0755</fileMode>
            </fileSet>
            <fileSet>
                <!-- ${env} 的值由 -P 的参数传递进来, 如:-P dev, 那么, ${env} 的值就是 dev -->
                <directory>${project.build.outputDirectory}/env/${env}/</directory>
                <includes>
                    <include>dubbo.properties</include>
                    <include>application.properties</include>
                    <include>conf.properties</include>
                    <include>logback.xml</include>
                    <include>dubbo-reference.xml</include>
                    <include>datasource/*/*</include>
                </includes>
                <outputDirectory>conf</outputDirectory>
                <fileMode>0644</fileMode>
            </fileSet>
        </fileSets>
            <!-- 将所有依赖包放入lib目录下 -->
        <dependencySets>
            <dependencySet>
                <outputDirectory>lib</outputDirectory>
            </dependencySet>
        </dependencySets>
    </assembly>
    

    modello-maven-plugin (在项目构建/生成源阶段、根据描述模型生成代码)

    参考:
    https://codehaus-plexus.github.io/modello/modello-maven-plugin/
    https://codehaus-plexus.github.io/modello/modello-maven-plugin/usage.html

    此配置将考虑版本1.0.0中文件src / main / mdo / myModel.mdo中描述的模型。它将在生成源阶段执行以下目标:

    • java将从模型生成Java bean,
    • xpp3-reader将为该模型创建一个XPP阅读器。

    生成的Java源代码将位于target / generated-sources / modello文件夹中(或由outputDirectory参数设置的文件夹中),该文件夹将作为源文件夹添加到项目构建周期中。
    在现场前阶段,它将执行以下目标:

    • XDOC这将产生在模型的一个文档目标/产生站点/ XDOC文件夹,这将反过来期间被处理,以输出在标准网站,网站相,
    • xsd将在target / generated-site / resources / xsd文件夹(由outputDirectory参数设置)中为我们的模型生成XML模式,该模式将在站点阶段复制到标准站点中。
    <project>
      [...]
      <build>
        <plugins>
          [...]
          <plugin>
            <groupId>org.codehaus.modello</groupId>
            <artifactId>modello-maven-plugin</artifactId>
            <version>1.10.0</version>
            <configuration>
              <models>
                <model>src/main/mdo/myModel.mdo</model>
              </models>
              <version>1.0.0</version>
            </configuration>
            <executions>
              <execution>
                <id>myModel</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>java</goal>
                  <goal>xpp3-reader</goal>
                </goals>
              </execution>
              <execution>
                <id>myModel-site-xsd</id>
                <phase>pre-site</phase>
                <goals>
                  <goal>xsd</goal>
                </goals>
                <configuration>
                  <outputDirectory>target/generated-site/resources/xsd</outputDirectory>
                </configuration>
              </execution>
              <execution>
                <id>myModel-site-doc</id>
                <phase>pre-site</phase>
                <goals>
                  <goal>xdoc</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          [...]
        </plugins>
      </build>
      [...]
    </project>
    

    相关文章

      网友评论

          本文标题:Maven pom

          本文链接:https://www.haomeiwen.com/subject/ulkoaqtx.html