用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模板

获取FTP文件最后修改时间

  • 如果FTP server和Web Server同一个时区
    使用方法org.apache.commons.net.ftp.FTPClient#getModificationTime

    public String getModificationTime(String pathname)
    throws IOException
    Issue the FTP MDTM command (not supported by all servers) to retrieve the last modification time of a file. The modification string should be in the ISO 3077 form “YYYYMMDDhhmmss(.xxx)?”. The timestamp represented should also be in GMT, but not all FTP servers honour this.
    Parameters:
    pathname – The file path to query.
    Returns:
    A string representing the last file modification time in YYYYMMDDhhmmss format.
    Throws:
    IOException – if an I/O error occurs.
    Since:
    2.0

    该函数会获取GMT时间,格式为:213 20160125035757。这时间和北京时间会差8个小时。按空格拆分字符串后使用如下方法获取北京时间20160125115757,主要是格式化添加时区sdf.setTimeZone(TimeZone.getTimeZone("GMT")),而不能按照默认的北京时间。因为FTP server和Web Server同一个时区,所以时间是一致的。

public static long getLongFromGMTDateString(String gmtDate, String format) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT")); //默认就是系统设置时区
        Date date = sdf.parse(gmtDate);
        return date.getTime();   // Unix时间戳
}

  • 如果FTP server和Web Server不在同一个时区
    需要结合另外第一个方法org.apache.commons.net.ftp.FTPFile#getModificationTime

    public Calendar getTimestamp()
    Returns the file timestamp. This usually the last modification time.
    Returns:
    A Calendar instance representing the file timestamp.

    这可以获取FTP时区的的最后修改时间,但是没有秒,结合方法getModificationTime获取秒,拼接起来就可以获取完整的FPT文件最后修改时间。

常识
  • Unix时间戳 1453694277000
    ->北京时间 2016/1/25 11:57:57
    -> GMT 2016/1/25 03:57:57
  • GMT –>北京时间
    GMT-> Unix时间戳 -> 北京时间

【心理/精神病?】《天才在左 疯子在右》

基本信息:

书名:天才在左 疯子在右

作者:高铭

出版社:北京联合出版公司出版  2016年1月

ISBN 978-7-5502-6393-2

<ps:这本书没看完,因为感觉到有点压抑,虽然在很多人看来比较好笑,嘲笑的笑,嘲笑作者的无知,嘲笑情节俗套。>

知乎里对这本书的评价比较全面,既然看不下去,也就不想多说了。这类的书,我怕是无缘了,或许是过于接近心理层面的思考,太孤独冷清了。

知乎评论链接http://www.zhihu.com/question/19811991

【社会万象】《莫泊桑 社会小说》

基本信息:

书名:莫泊桑 社会小说 

作者:莫泊桑(法国)

译者:郭宏安

出版社:上海文艺出版社 2012年4月第一版

ISBN 978-7-5321-4369-6/I·3384

丛书:新文艺 外国文学大师读本

    

<听说的关于莫泊桑的简评>所谓“迷人”,指的是阅读时的愉悦,思考时的痛苦,因为莫泊桑的小说世界是一个痛苦多,欢乐少,笼罩着一片悲观主义的凉雾世界。“其境过清,不可久居”,久居则有“凄神寒骨”之虞。

1.羊脂球

     羊脂球一直在哭,有时候在两节歌声之间,黑暗里送出一声呜咽,那是她没能忍住的一声悲啼。

2.菲菲小姐

3.两个朋友

     “只要世界上还有政府,这种情况就永远不会改变。”

     “有了国王,我们就要同外国打仗;有了共和国,我们就要打内仗。”

     人类永远不能得到自由。

4.索瓦热老婆婆

     简介:一位老婆婆和四位敌国兵的爱恨交织,结局是老婆婆在得知自己的儿子死在战场之后,决心烧死在自己家里居住的四位年轻的敌国的士兵。故事发生在老婆婆居住的村子被敌军占领之后。

     一种失之交臂的幸福感觉。

    因为乡下人米有什么爱国心激起的仇恨;那只有上层阶级的人才有。

    “为了报复,德国人才把我的那座城堡毁掉。”可是我这时候却在想那四个烧死在这座茅屋里的善良的小伙子的母亲,想另一位被杀在这堵墙边的母亲的残忍的英勇行为。我捡起来一块小石头,上面还留着被火燎过的黑颜色。

5.我的叔叔于勒

     ……这个可怜的人,这时候总做出一个手势,叫我看了心里十分难过。他总是张开了手摸一下额头,好像要抹去根本不存在的汗珠,并且总是一句话也不回答。

     行为的好坏,只有结果才能决定。

     在有钱人的家里,一个人吃喝玩乐,无非算是糊涂荒唐。大家笑嘻嘻地称呼他一声花花公子。在生活困难的家庭里,一个人要是逼得父母动老本,那他就是一个坏蛋,一个流氓,一个无赖了。<ps:这句话被一位朋友反驳:这句话有偷换概念之嫌,因为在有钱人的家里,一个人要是逼得父母动老本,那他就是败家子,不仅仅是糊涂荒唐这么简单了。>

6.项链

     <关于虚荣>她很虚荣,但是她很美丽;她快乐地忘乎所以,也彬彬有礼。可她为什么选择了他?害了自己,也害了他。>现实中有种生活叫“门当户对”,也有蒂凡尼的早餐。

7.图瓦

      “怎么样,图瓦老爹,红焖第一口鸡的时候可得请我啊,请不请?”   “当然得请你,我的姑爷。” (图瓦老爹喜欢见谁都叫姑爷,这鸡是图瓦老爹自己在被子里孵出来的,他就像这些小鸡的母亲)

8.港口

9.上校的意义

10.模特儿

     画家在妻子的车旁默默走着,一小时以来,他们之间连一句话也没有交谈过。<错误>

<完>

jboss-eap-6.2 JNDI配置数据源Oracle&sql server

jboss-eap-6.2 JNDI配置数据源Oracle&sql server

  • Oracle数据源
  1. 创建模块,jboss-eap-6.2\modules\system\layers\base目录下,创建目录com\oracle\main
  2. 拷贝ojdbc14-10.2.0.4.0.jar和module.xml

module.xml

<?xml version="1.0" encoding="UTF-8"?>  
<module xmlns="urn:jboss:module:1.1" name="com.oracle">  
    <properties>  
        <property name="jboss.api" value="public"/>  
    </properties>  
    <resources>  
        <resource-root path="ojdbc14-10.2.0.4.0.jar"/>  
        <!-- Insert resources here -->  
    </resources>  
    <dependencies>  
        <module name="javax.api"/>  
        <module name="javax.transaction.api"/>  
        <module name="javax.servlet.api" optional="true"/>  
    </dependencies>  
</module>  
  1. 修改jboss-eap-6.2\standalone\configuration\standalone.xml

standalone.xml

<datasource jta="true" jndi-name="java:jboss/datasources/OracleDS" pool-name="OracleDS" enabled="true" use-java-context="true">
    <connection-url>jdbc:oracle:thin:@127.0.0.1:1521:ORCL</connection-url>
    <driver>oracle</driver>
    <pool>
        <prefill>false</prefill>
        <use-strict-min>false</use-strict-min>
        <flush-strategy>FailingConnectionOnly</flush-strategy>
    </pool>
    <security>
        <user-name>rick</user-name>
        <password>jkxyx205</password>
    </security>
</datasource>
<driver name="oracle" module="com.oracle">
    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
</driver>
  • sql server数据源同上,仅给出配置文件sql server

module.xml

<?xml version="1.0" encoding="UTF-8"?>  
<module xmlns="urn:jboss:module:1.1" name="com.microsoft.sqlserver.jdbc">  
    <properties>  
        <property name="jboss.api" value="public"/>  
    </properties>  
    <resources>  
        <resource-root path="sqljdbc4.jar"/>  
        <!-- Insert resources here -->  
    </resources>  
    <dependencies>  
        <module name="javax.api"/>  
        <module name="javax.transaction.api"/>  
        <module name="javax.servlet.api" optional="true"/>  
    </dependencies>  
</module>  
<driver name="sqlserver" module="com.microsoft.sqlserver.jdbc">
    <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
</driver>
  • 注意点模块下包的结构和name对应起来

  • Spring使用JNDI数据源

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd


http://www.springframework.org/schema/aop


http://www.springframework.org/schema/aop/spring-aop.xsd


http://www.springframework.org/schema/tx


http://www.springframework.org/schema/tx/spring-tx.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context.xsd

    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
    ">

   <jee:jndi-lookup id="myDataSource1" jndi-name="java:jboss/datasources/OracleDS" />
   <jee:jndi-lookup id="myDataSource2" jndi-name="java:jboss/datasources/MssqlDS" />
</beans>
  • 控制台查看数据源

http://127.0.0.1:9990

如果无法进入控制台,需要配置用户和组,执行jboss-eap-6.2\bin\add-user.bat,按照提示执行。

http://xhope.top/wp-content/uploads/2016/02/1.jpg