Maven2 学习笔记[5]-构建一个Java Application项目

Maven2 学习笔记[5]-构建一个Java Application项目

这个 Java App 项目,包含的东西还是蛮多的。
在我构建这个项目的过程中遇到了很多问题,让我着实的痛苦了一天,在这一天中,我在网上看到很多构建类似项目的文章,但写的都不是很详细。至少不能满足我的需求。
对于像我这样,需要在1天内就要搞定打包的人来讲,这确实很失望。
所以我要把我构建这个项目的过程,记录下来,希望能对 与我有类似需求的人 有一些帮助。
里面的一些配置也许不够完美,还请指正。谢谢。

1.我的需求
我要构建一个项目,打好jar包后,我希望的是:
a.执行命令 java -jar QrtzPrj.jar 便可执行。(即要在manifest文件中指定main函数;PS:我打包的名字为QrtzPrj.jar)
b.在生成好的jar包同一目录下,有lib文件夹,此文件夹下放置此项目所依赖的所有jar包。
c.在生成好的jar包同一目录下,有config文件夹,此文件夹放置此项目的配置文件。包含以下文件:
applicationContext.xml、jdbc.properties、log4j.xml、quartz.properties、sql-map-config.xml;
并且要将config文件夹也加入到classpath中。(在MyEclipse中开发时,便将这些文件加入到classpath中)
d.配置文件不能打入到QrtzPrj.jar中,因为当此程序发布后,如要修改配置信息,如数据库的连接信息等,可直接停止程序,修改配置文件,再启动程序便可,省去了重新打包的过程。
e.要打包的代码中,包含了xml文件。(此xml文件用于配置iBatis执行的SQL语句)

2.项目结构目录

src/main/java 下面便是代码,包为 com.jn.***其他省略。
其中在com.jn.persistence.sql目录下,有文件 sql.xml,这里配置了iBatis要执行的SQL语句。


3.打包错误1
当我的项目都开发好后,开始进行打包。
我的操作方法是,拿一个基础的pom.xml文件进行修改。修改后的pom文件如下:
清单1:

< 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/xsd/maven-4.0.0.xsd" >
    
< modelVersion > 4.0.0 </ modelVersion >
    
< groupId > com.jn </ groupId >
    
< artifactId > QrtzPrj </ artifactId >
    
< version > 1.0 </ version >
    
< packaging > jar </ packaging >
    
< name > QrtzPrj </ name >
    
< url > http://maven.apache.org </ url >
    
< properties >
        
< project .build.sourceEncoding > UTF-8 </ project.build.sourceEncoding >
    
</ properties >
    
< build >
        
<!--  指定生成jar包的名称  -->
        
< finalName > QrtzPrj </ finalName >
        
< plugins >
            
< plugin >
                
< groupId > org.apache.maven.plugins </ groupId >
                
< artifactId > maven-compiler-plugin </ artifactId >
                
< configuration >
                    
< source > 1.6 </ source >
                    
< target > 1.6 </ target >
                
</ configuration >
            
</ plugin >
            
< plugin >
                
< groupId > org.apache.maven.plugins </ groupId >
                
< artifactId > maven-jar-plugin </ artifactId >
                
< goals >
                    
< goal > jar </ goal >
                
</ goals >
                
< configuration >
                    
< archive >
                        
< manifest >
                            
< mainClass > com.jn.common.Main </ mainClass >
                            
< addClasspath > true </ addClasspath >
                        
</ manifest >
                    
</ archive >
                
</ configuration >
            
</ plugin >
            
< plugin >
                
< groupId > org.apache.maven.plugins </ groupId >
                
< artifactId > maven-surefire-plugin </ artifactId >
                
< configuration >
                    
< testFailureIgnore > true </ testFailureIgnore >
                
</ configuration >
            
</ plugin >
            
< plugin >
                
< artifactId > maven-assembly-plugin </ artifactId >
                
< configuration >
                    
< descriptorRefs >
                        
< descriptorRef > jar-with-dependencies </ descriptorRef >
                    
</ descriptorRefs >
                
</ configuration >
            
</ plugin >
        
</ plugins >
        
< resources >
            
< resource >
                
< directory > src/main/resources </ directory >
            
</ resource >
        
</ resources >
    
</ build >
    
< dependencies >
        
< dependency >
            
< groupId > org.aspectj </ groupId >
            
< artifactId > aspectjweaver </ artifactId >
            
< version > 1.6.6 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > org.springframework </ groupId >
            
< artifactId > spring </ artifactId >
            
< version > 2.5.5 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > org.apache.ant </ groupId >
            
< artifactId > ant-antlr </ artifactId >
            
< version > 1.8.1 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > commons-beanutils </ groupId >
            
< artifactId > commons-beanutils </ artifactId >
            
< version > 1.7.0 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > commons-collections </ groupId >
            
< artifactId > commons-collections </ artifactId >
            
< version > 3.2.1 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > commons-dbcp </ groupId >
            
< artifactId > commons-dbcp </ artifactId >
            
< version > 1.3 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > commons-dbutils </ groupId >
            
< artifactId > commons-dbutils </ artifactId >
            
< version > 1.2 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > commons-io </ groupId >
            
< artifactId > commons-io </ artifactId >
            
< version > 1.4 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > commons-lang </ groupId >
            
< artifactId > commons-lang </ artifactId >
            
< version > 2.4 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > commons-logging </ groupId >
            
< artifactId > commons-logging </ artifactId >
            
< version > 1.0.4 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > commons-net </ groupId >
            
< artifactId > commons-net </ artifactId >
            
< version > 2.0 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > commons-pool </ groupId >
            
< artifactId > commons-pool </ artifactId >
            
< version > 1.5.4 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > ibatis </ groupId >
            
< artifactId > ibatis </ artifactId >
            
< version > 2.3.4.726 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > jsonlib </ groupId >
            
< artifactId > jsonlib </ artifactId >
            
< version > 2.3 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > com.sdicons.jsontools </ groupId >
            
< artifactId > jsontools-core </ artifactId >
            
< version > 1.7 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > log4j </ groupId >
            
< artifactId > log4j </ artifactId >
            
< version > 1.2.15 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > net.sf.ezmorph </ groupId >
            
< artifactId > ezmorph </ artifactId >
            
< version > 1.0.6 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > ojdbc </ groupId >
            
< artifactId > ojdbc </ artifactId >
            
< version > 14 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > org.quartz-scheduler </ groupId >
            
< artifactId > quartz-all </ artifactId >
            
< version > 1.8.3 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > org.slf4j </ groupId >
            
< artifactId > slf4j-api </ artifactId >
            
< version > 1.5.10 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > org.slf4j </ groupId >
            
< artifactId > slf4j-log4j12 </ artifactId >
            
< version > 1.5.10 </ version >
        
</ dependency >
    
</ dependencies >
</ project >

a.报oracle驱动jar包的错误。因为maven去下载oracle驱动jar包无法获取成功,所以会报此错误。
这个只要根据学习笔记4:http://www.blogjava.net/jnbzwm/archive/2010/09/03/330879.html 搭建自己的私服,然后下载jar,自己上传便可以解决。
b.报以下3个包的错误:
com.sun.jdmk:jmxtools:jar:1.2.1
com .sun .jmx:jmxri:jar:1.2.1
javax.jms:jms:jar:1.1
看了maven的错误提示,大概了解了,是因为引入了高版本的log4j的包(1.2.15的包会依赖,1.2.14及以前的不会),依赖了以上这些包,而这3个jar包maven下载获取又没成功。于是我还是按照老办法,自己去下载,并上传至本地私服,修改了groupId、artifactId、version后,再进行 mvn package。
居然还是报错。后来分析了一下,原来是这样:
这3个包因为需要一些认证,maven并没有获取成功,但是在本地仓库中,已经存在了\repository\javax\jms\jms\1.1\jms-1.1.jar 等等这些jar了。(本地仓库的jar包存放路径由groupId、artifactId组成)
只是这些jar包都是没有内容的,于是我将我手动下载的这些jar包,覆盖到了它们相应目录下。重新进行打包,OK了,不报错了。打包成功。
但看一下打包后的情况,根本不符合我的要求:
a.在QrtzPrj.jar的同一目录下并没有lib文件夹,也没有依赖的jar包。
b.没有config文件夹。
c.配置文件都被打在jar里面。(这是由maven默认的,它会找到src/main/resource目录下的配置,将其打在jar中)
d.manifest文件完全不是我要求的。

不过没关系,知道了差距,修改pom.xml文件即可。
待续....

本文为原创,欢迎转载,转载请注明出处BlogJava。

你可能感兴趣的:(Maven2 学习笔记[5]-构建一个Java Application项目)