从一份配置清单详解Maven配置

2019-05-17  本文已影响0人  superxcp
maven.png

一.概述:

这是一份我们maven项目使用的详细配置,今天我们以这份配置文件详细介绍下maven的使用方法。

<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/maven-v4_0_0.xsd">
    <parent>
        <groupId>cn.kaka</groupId>
        <artifactId>kaka_cms_parent</artifactId>
        <version>1.0.0</version>
        <relativePath>../kaka_cms_parent/pom.xml</relativePath>
    </parent>

    <groupId>cn.kaka</groupId>
    <artifactId>kaka_cms_admin</artifactId>
    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>

    <properties>
        <!-- 项目编码 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--JDK 版本 -->
        <project.jdk.version>1.8</project.jdk.version>
        <!-- kaka cms service -->
        <kaka_cms_service.version>1.0.0</kaka_cms_service.version>
        <jetty.version>9.2.8.v20150217</jetty.version>
        <maven.build.timestamp.format>yyyyMMddHHmm</maven.build.timestamp.format>
        <project.name>kaka_cms_admin</project.name>
    </properties>
    <dependencies>
        <!-- kaka cms service -->
        <dependency>
            <groupId>cn.kaka</groupId>
            <artifactId>kaka_cms_service</artifactId>
            <version>${kaka_cms_service.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.14.RELEASE</version>
        </dependency>

        <!-- druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <!-- poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
        </dependency>
        <!-- commons fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- commons io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.kaka.nps</groupId>
            <artifactId>kaka-nps-sdk</artifactId>
            <version>1.5.1</version>
        </dependency>

        <dependency>
            <groupId>inner-passport</groupId>
            <artifactId>inner-passport</artifactId>
            <version>1.0.1</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>${commons-logging.version}</version>
        </dependency>

        <dependency>
            <groupId>httpcore</groupId>
            <artifactId>httpcore</artifactId>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>com.kaka.stat</groupId>
            <artifactId>stat-open-api-sdk</artifactId>
            <version>1.1.1</version>
        </dependency>

        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        ......

    </dependencies>

    <build>
        <finalName>${project.name}_${maven.build.timestamp}</finalName>
        <filters>
            <filter>src/main/filters/filter-${env}.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.txt</include>
                    <include>**/*.ftl</include>
                    <include>**/*.js</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/java</directory>
                <includes>
                    <include>**/*.json</include>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xsd</include>
                    <include>**/*.js</include>
                </includes>
            </testResource>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
        </testResources>
        <extensions>
            <extension>
                <groupId>org.apache.maven.wagon</groupId>
                <artifactId>wagon-ssh</artifactId>
                <version>2.8</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>${project.jdk.version}</source>
                    <target>${project.jdk.version}</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId> 
                <version>2.3</version>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>src/main/webapp/WEB-INF</directory>

                            <filtering>true</filtering> 
                            <targetPath>WEB-INF</targetPath>                    
                        </resource>
                    </webResources>                 
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty.version}</version>
                <configuration>
                    <!--<scanIntervalSecond>10</scanIntervalSecond>-->
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                    <stopKey>exit</stopKey>
                    <stopPort>9998</stopPort>
                    <httpConnector>
                        <port>9089</port>
                    </httpConnector>
                    <!--<encoding>UTF-8</encoding>-->
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>wagon-maven-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                    <fromFile>target/${project.name}_${maven.build.timestamp}.war</fromFile>
                    <url>scp://devftp:ztemt123@192.168.4.250/data/kaka_cms/${env}/</url>
                    <commands>
                        <command> echo ${project.name}_${maven.build.timestamp}.war >> /data/kaka_cms/${env}/cms_admin_${env}.log</command>
                    </commands>
                    <displayCommandOutputs>true</displayCommandOutputs>
                </configuration>
            </plugin>

            <!--maven 使用surefure进行测试 可以执行testng或junit脚本-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <env>product</env>
            </properties>
        </profile>
    </profiles>
</project>

1.本项目信息

    <groupId>cn.kaka</groupId>
    <artifactId>kaka_cms_admin</artifactId>
    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>

首先,这个项目是卡卡公司(公司名不便透露,暂时以kaka为公司名代替)的项目,项目名kaka_cms_admin,是一个后台项目,版本号4.0.0,如果别的项目想依赖使用admin这个项目,dependency标签里面的三个信息必须跟这个信息相同,否则引用不到。最后打成的包是war包,可以运行为web项目的包。

2.maven pom文件之间的关系

    <parent>
        <groupId>cn.kaka</groupId>
        <artifactId>kaka_cms_parent</artifactId>
        <version>1.0.0</version>
        <relativePath>../kaka_cms_parent/pom.xml</relativePath>
    </parent>

maven管理的每个项目都是被当做对象看待的,maven项目之间的关系有依赖、继承和聚合三种关系。

    <modules>
        <module>../kaka_jee_framework</module>
        <module>../kaka_cms_admin</module>
        <module>../kaka_cms_api</module>
        <module>../kaka_cms_service</module>
        <module>../kaka_cms_task</module>
    </modules>

3. property属性

    <properties>
        <!-- 项目编码 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--JDK 版本 -->
        <kaka_cms_service.version>1.0.0</kaka_cms_service.version>
        ......
    </properties>

property是自己命名的属性,就像是java中我们自己命名变量一样,如果想在pom文件中使用该属性,使用${kaka_cms_service.version}这种引用方式。设计这种变量的意义在于,如果很多子项目引用了父项目的jar包,要改下引用jar包的版本(例如修改jdk版本),只要改这个属性的变量值就ok了,不用所有项目中的值都改一遍。引用如下:

       <dependency>
            <groupId>cn.kaka</groupId>
            <artifactId>kaka_cms_service</artifactId>
            <version>${kaka_cms_service.version}</version>
        </dependency>

4. <profile>属性

    <profiles>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <env>product</env>
            </properties>
        </profile>
    </profiles>

定义了各个环境的变量id,我们这里有test环境和product环境。

5. <build>属性

<finalName>${project.name}_${maven.build.timestamp}</finalName>
<filter>src/main/filters/filter-${env}.properties</filter>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.txt</include>
                    <include>**/*.ftl</include>
                    <include>**/*.js</include>
                </includes>
            </resource>
<build>
    <extensions>
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ssh</artifactId>
            <version>2.8</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>wagon-maven-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                    <fromFile>target/${project.name}_${maven.build.timestamp}.war</fromFile>
                    <url>scp://user:password@192.168.4.250/data/kaka_cms/${env}/</url>
                    <commands>
                        <command> echo ${project.name}_${maven.build.timestamp}.war >> /data/kaka_cms/${env}/cms_admin_${env}.log</command>
                    </commands>
                    <displayCommandOutputs>true</displayCommandOutputs>
                </configuration>
            </plugin>
    </plugins>
</build>

wagon程序开发完成后部署到线上linux机器,可能需要经历打包。将包文件传到服务器,ssh连上服务器,桥命令启动程序等一系列繁琐的步骤,实际上这些步骤都可以通过maven的一个插件wagon-maven-plugin来自动完成。其中:
<fromFile>target/{project.name}_{maven.build.timestamp}.war</fromFile>是要上传到服务器的文件;
<url>scp://user:password@192.168.4.250/data/kaka_cms/${env}/</url>配置服务器的用户、密码、地址以及文件上传的目录。
配置完成后,运营命令:

mvn clean install -Dmaven.test.skip=true -P test wagon:upload-single wagon:sshexec

解释下命令:
-Dmaven.test.skip=true --- 不执行测试用例,也不编译测试用例类;
-P test ---测试环境(P是profile的意思);
wagon:upload-single --- 上传单文件;
wagon:sshexec --- 运行command命令;
(我们这里会把war文件的名字上传到一个log文件中,机器定时检测该log文件,如果有新包更新,就会去这个/data/kaka_cms/${env}/文件中取包进行发布)

后记:由于能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流!

上一篇 下一篇

猜你喜欢

热点阅读