用Maven分环境打包非Maven项目

     现在有一个项目,不是用maven构建的,想通过maven进行各个环境的打包,比如dev/sit/prd环境jdbc.properties是不相同的。不能破坏原来的目录结构,同时实现以下功能:
1. 多环境打包
2. 结合ant插件清理临时文件,支持war上传FTP

工程目录

修改如下
* 创建文件夹config放置文件jdbc.properties和打包所需要的provided jar
* 添加pom.xml
* bat执行脚本

1 .配置文件目录

config是在项目的一级目录,和src同一级

|-config
  |-LOCAL_HK
     |-jdbc.properties
  |-SIT_HK
     |-jdbc.properties

2. pom.xml

<?xml version="1.0" encoding="UTF-8"?>  
<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"> 
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.asw.spacemgt</groupId>
    <artifactId>asw</artifactId>
    <version>V1.5.0.${maven.build.timestamp}</version>
    <packaging>war</packaging>
    <name>SpaceMgt</name>

    <properties>
        <maven.build.timestamp.format>yyyyMMdd</maven.build.timestamp.format>
        <!-- 文件拷贝时的编码 -->  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
        <!-- 编译时的编码 -->  
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding> 
        <!--ftp配置信息 -->
        <ftp.server>172.18.50.126</ftp.server>
        <ftp.userid>alex</ftp.userid>
        <ftp.password>movit123.</ftp.password>
    </properties>

     <build>
        <!-- java code -->
        <sourceDirectory>src</sourceDirectory>  

        <resources> 
            <resource>
                <!-- 
                获取资源文件
                *.hbm.xml
                *.properties

                排除java文件+要替换的文件
                -->
                <directory>src</directory>
                <excludes>  
                    <exclude>**/jdbc.properties</exclude>  
                    <exclude>**/space.properties</exclude>  
                    <exclude>**/*.java</exclude>  
                </excludes>
            </resource>
            <resource>
                <!--执行文件替换-->
                <directory>config/${env}</directory>
                <targetPath>resource</targetPath>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <configuration>  
                    <source>1.7</source>  
                    <target>1.7</target>  
                    <encoding>UTF-8</encoding>
                    <compilerArguments>
                        <!--本地jar-->
                        <extdirs>WebContent\WEB-INF\lib</extdirs>
                    </compilerArguments>
                </configuration>  
            </plugin>  
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>  
                    <webResources>  
                        <resource>      
                            <!--web app位置-->
                            <directory>WebContent</directory> 
                        </resource> 
                    </webResources> 

                    <webXml>WebContent\WEB-INF\web.xml</webXml>
                </configuration>  
            </plugin>
            <plugin>   
               <artifactId>maven-antrun-plugin</artifactId>    
               <version>1.7</version>  
               <executions>       
                    <execution>    
                               <id>default-cli</id>                 
                               <goals>            
                                     <goal>run</goal>        
                               </goals>             
                               <configuration>    
                                    <target>
                                        <!--删除maven打包产生的临时目录-->
                                        <delete includeemptydirs="true">
                                            <fileset dir="deploy" >
                                                <exclude name="${maven.build.timestamp}/**/*.war"/>
                                            </fileset>
                                        </delete>
                                         <!--通过ftp创建文件夹--> 
                                        <ftp server="${ftp.server}" userid="${ftp.userid}" password="${ftp.password}" action="mkdir" remotedir="/Space/war/sit/${maven.build.timestamp}"></ftp>
                                        <!--上传到FTP-->
                                        <ftp server="${ftp.server}" action="put" passive="true" remotedir="/Space/war/sit/${maven.build.timestamp}" userid="${ftp.userid}" 
                                        password="${ftp.password}"  separator="/" verbose="yes" binary="yes">  
                                             <fileset dir="deploy/${maven.build.timestamp}/SIT">  
                                             </fileset>  
                                         </ftp>  
                                    </target>
                               </configuration>        
                    </execution>    
                </executions>  
                <!--上传ftp依赖的jar-->
                <dependencies>                
                     <dependency>  
                        <groupId>commons-net</groupId>  
                        <artifactId>commons-net</artifactId>  
                        <version>3.3</version>  
                        <scope>system</scope>  
                        <systemPath>${project.basedir}/config/lib/commons-net-3.3.jar</systemPath>  
                     </dependency>  
                     <dependency>  
                        <groupId>org.apache.ant</groupId>  
                        <artifactId>ant-commons-net</artifactId>  
                        <version>1.0</version>  
                        <scope>system</scope>  
                        <systemPath>${project.basedir}/config/lib/ant-commons-net.jar</systemPath>  
                     </dependency>  
                </dependencies>
            </plugin>  
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>LOCAL_HK</id>
            <properties>
                <env>LOCAL_HK</env>
                <buCode>HK</buCode>
            </properties>
            <build>
                <finalName>SpaceMgt_${buCode}</finalName>
                <directory>deploy/${maven.build.timestamp}/LOCAL</directory>
            </build>
        </profile> 
        <profile>
            <id>SIT_HK</id>
            <properties>
                <env>SIT_HK</env>
                <buCode>HK</buCode>
            </properties>
            <build>
                <finalName>SpaceMgt_${buCode}</finalName>
                <directory>deploy/${maven.build.timestamp}/SIT</directory>
            </build>
        </profile> 
    </profiles>

    <dependencies>
        <!-- lib不存在javax相关jar,编译期使用,不会打到war中 -->
        <dependency>  
            <groupId>jboss</groupId>  
            <artifactId>servlet</artifactId>  
            <version>1.0</version>  
            <scope>system</scope>  
            <systemPath>${project.basedir}/config/lib/jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar</systemPath>  
        </dependency>  
        <dependency>  
            <groupId>jboss</groupId>  
            <artifactId>jsp</artifactId>  
            <version>1.0</version>  
            <scope>system</scope>  
            <systemPath>${project.basedir}/config/lib/jboss-jsp-api_2.2_spec-1.0.1.Final-redhat-2.jar</systemPath>  
        </dependency>  
        <dependency>  
            <groupId>jboss</groupId>  
            <artifactId>jstl</artifactId>  
            <version>1.0</version>  
            <scope>system</scope>  
            <systemPath>${project.basedir}/config/lib/jboss-jstl-api_1.2_spec-1.0.3.Final-redhat-2.jar</systemPath>  
        </dependency>  
    </dependencies>
</project>  

3. bat脚本

编写bat脚本build.bat

@echo build war
call mvn package -PLOCAL_HK
call mvn package -PSIT_HK
call mvn antrun:run

双击build.bat文件会在deploy文件下分目录产生,同时上传FTP。

最终目录结构:

相关链接:

ant构建java项目
IDEA开发 Maven打包可执行的jar JSmooth转exe文件
build.xml模板