ALL is Well!

敏捷是一条很长的路,摸索着前进着

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  30 随笔 :: 23 文章 :: 71 评论 :: 0 Trackbacks

接上一篇Maven2 学习笔记[5]-构建一个Java Application项目。
一个一个来处理不符合我们需求的地方。

在我的博客 学习资源库 链接里,有介绍Maven插件的,对于以下不清楚的插件用法、节点用法,可以去查一下,写的很明确,而且还有例子。
我就是在那里一边看用法,一边写pom.xml文件。

1.问题:在QrtzPrj.jar的同一目录下并没有lib文件夹,也没有依赖的jar包。
这个问题的解决办法便是引入Maven的maven-dependency-plugin插件。
内容如下:(配置在<build> <plugins>...</pulugins></build> 中)

            <plugin>
                
<groupId>org.apache.maven.plugins</groupId>
                
<artifactId>maven-dependency-plugin</artifactId>
                
<executions>
                    
<execution>
                        
<id>copy-dependencies</id>
                        
<phase>package</phase>
                        
<goals>
                            
<goal>copy-dependencies</goal>
                        
</goals>
                        
<configuration>
                            
<outputDirectory>${project.build.directory}/lib</outputDirectory>
                            
<overWriteReleases>false</overWriteReleases>
                            
<overWriteSnapshots>false</overWriteSnapshots>
                            
<overWriteIfNewer>true</overWriteIfNewer>
                        
</configuration>
                    
</execution>
                
</executions>
            
</plugin>

保存pom.xml,再次进行打包。嗯,不错,lib文件夹产生了,所依赖的jar也都拷贝过去了。

2.问题:没有config文件夹及配置文件;且配置文件被打在jar包中了。
这个问题与第一个问题类似,这次需要引入maven的maven-resources-plugin 插件。
为了不让配置文件打在jar中,还需要对maven-jar-plugin 进行一些配置。
a. maven-jar-plugin的配置内容:(在<plugin><configuration>...</configuration></plugin>中配置)
                    <excludes>
                        
<exclude>applicationContext.xml</exclude>
                        
<exclude>log4j.xml</exclude>
                        
<exclude>jdbc.properties</exclude>
                        
<exclude>quartz.properties</exclude>
                        
<exclude>sql-map-config.xml</exclude>
                    
</excludes>

b. maven-resources-plugin 插件的配置内容如下:
            <plugin>
                
<groupId>org.apache.maven.plugins</groupId>
                
<artifactId>maven-resources-plugin</artifactId>
                
<version>2.3</version>
                
<executions>
                    
<execution>
                        
<id>copy-resources</id>
                        
<phase>package</phase>
                        
<goals>
                            
<goal>copy-resources</goal>
                        
</goals>
                        
<configuration>
                            
<encoding>UTF-8</encoding>
                            
<outputDirectory>${project.build.directory}/config</outputDirectory>
                            
<resources>
                                
<resource>
                                    
<directory>src/main/resources</directory>
                                    
<includes>
                                        
<include>applicationContext.xml</include>
                                        
<include>log4j.xml</include>
                                        
<include>jdbc.properties</include>
                                        
<include>quartz.properties</include>
                                        
<include>sql-map-config.xml</include>
                                    
</includes>
                                    
<filtering>true</filtering>
                                
</resource>
                            
</resources>
                        
</configuration>
                    
</execution>
                
</executions>
            
</plugin>

保存pom.xml,再进行打包。
lib文件夹及依赖包,config文件夹及配置文件都拷贝过去了。再打开QrtzPrj.jar看一下,配置文件没有被打在其中,很好。
接下来看其他的问题。

3.问题:manifest文件完全不是我要求的。
现在的manifest文件内容如下:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Administrator
Build-Jdk: 1.6.0_13
Main-Class: com.jn.common.Main
Class-Path: aspectjweaver-1.6.6.jar spring-2.5.5.jar commons-logging-1
 .0.4.jar ant-antlr-1.8.1.jar commons-beanutils-1.7.0.jar commons-coll
 ections-3.2.1.jar commons-dbcp-1.3.jar commons-pool-1.5.4.jar commons
 -dbutils-1.2.jar commons-io-1.4.jar commons-lang-2.4.jar commons-net-
 2.0.jar ibatis-2.3.4.726.jar jsonlib-2.3.jar jsontools-core-1.7.jar a
 ntlr-2.7.7.jar log4j-1.2.15.jar mail-1.4.jar activation-1.1.jar jms-1
 .1.jar jmxtools-1.2.1.jar jmxri-1.2.1.jar ezmorph-1.0.6.jar ojdbc-14.
 jar quartz-all-1.8.3.jar slf4j-api-1.5.10.jar slf4j-log4j12-1.5.10.jar

1)classPath中,每个jar文件前应限定文件夹名,即应该加入 lib/。
2)config文件夹并没有被加入到classpath中。
好,查资料,既然是打包时候manifest文件不满足要求,当然要看 maven-jar-plugin 插件了。继续研究它吧。

a.将 maven-jar-plugin 插件的<manifest>节点修改为:
                        <manifest>
                            
<mainClass>com.jn.common.Main</mainClass>
                            
<addClasspath>true</addClasspath>
                            
<!-- 追加前缀 -->
                            
<classpathPrefix>lib/</classpathPrefix>
                        
</manifest>

b.在<archive>节点内增加以下内容:
                        <manifestEntries>
                            
<Class-Path>config/</Class-Path>
                        
</manifestEntries>

好,保存pom.xml,再进行打包。
manifest文件变成了:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Administrator
Build-Jdk: 1.6.0_13
Main-Class: com.jn.common.Main
Class-Path: config/ lib/aspectjweaver-1.6.6.jar lib/spring-2.5.5.jar l
 ib/commons-logging-1.0.4.jar lib/ant-antlr-1.8.1.jar lib/commons-bean
 utils-1.7.0.jar lib/commons-collections-3.2.1.jar lib/commons-dbcp-1.
 3.jar lib/commons-pool-1.5.4.jar lib/commons-dbutils-1.2.jar lib/comm
 ons-io-1.4.jar lib/commons-lang-2.4.jar lib/commons-net-2.0.jar lib/i
 batis-2.3.4.726.jar lib/jsonlib-2.3.jar lib/jsontools-core-1.7.jar li
 b/antlr-2.7.7.jar lib/log4j-1.2.15.jar lib/mail-1.4.jar lib/activatio
 n-1.1.jar lib/jms-1.1.jar lib/jmxtools-1.2.1.jar lib/jmxri-1.2.1.jar 
 lib/ezmorph-1.0.6.jar lib/ojdbc-14.jar lib/quartz-all-1.8.3.jar lib/s
 lf4j-api-1.5.10.jar lib/slf4j-log4j12-1.5.10.jar

good!这次应该没问题了。准备执行吧!

4.执行后报错
大意就是 iBatis 找不到在 sql-map-client.xml配置的 文件路径<sqlMap resource="com/jn/persistence/sql/sql.xml" />
于是我到jar包中去查找 此xml文件,果然没有。
这下郁闷了,又查了下资料,还得修改 maven-jar-plugin 插件的配置。
在<configuration>节点下这样配置:(bin/是编译后输出的路径)
<classesDirectory>bin/</classesDirectory>

好,我再打包试试看。打好后,去看了一下com/jn/persistence/sql 目录,嗯,good,sql.xml也在了。
我想这下 java -jar QrtzPrj.jar 可以派上用场了吧。

5.执行后又报错
我当时已经快崩溃了。报的错误大意是 无法用 http://maven.apache.org  连接到数据库。
嗯,很熟悉的url,它就配置在我们的pom.xml中<project>节点内:
<url>http://maven.apache.org</url>

大概知道了原因,jdbc.properties文件 和 applicationContext.xml文件有placeholder的操作。
好吧,我把jdbc.properties里配置的 url=jdbc:oracle:thin:@localhost:1521:root 修改一下,把url改成dburl,相应的 applicationContext.xml 里也改一下。
(也许这里可以不向maven妥协,希望高人能指点一下,可以让maven打包时不做这样的替换。)
再执行,终于OK了。
我感叹:maven 打包做的事情还真多啊!!

最后附上我的pom.xml文件:
<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-dependency-plugin</artifactId>
                
<executions>
                    
<execution>
                        
<id>copy-dependencies</id>
                        
<phase>package</phase>
                        
<goals>
                            
<goal>copy-dependencies</goal>
                        
</goals>
                        
<configuration>
                            
<outputDirectory>${project.build.directory}/lib</outputDirectory>
                            
<overWriteReleases>false</overWriteReleases>
                            
<overWriteSnapshots>false</overWriteSnapshots>
                            
<overWriteIfNewer>true</overWriteIfNewer>
                        
</configuration>
                    
</execution>
                
</executions>
            
</plugin>
            
<plugin>
                
<groupId>org.apache.maven.plugins</groupId>
                
<artifactId>maven-resources-plugin</artifactId>
                
<version>2.3</version>
                
<executions>
                    
<execution>
                        
<id>copy-resources</id>
                        
<phase>package</phase>
                        
<goals>
                            
<goal>copy-resources</goal>
                        
</goals>
                        
<configuration>
                            
<encoding>UTF-8</encoding>
                            
<outputDirectory>${project.build.directory}/config</outputDirectory>
                            
<resources>
                                
<resource>
                                    
<directory>src/main/resources</directory>
                                    
<includes>
                                        
<include>applicationContext.xml</include>
                                        
<include>log4j.xml</include>
                                        
<include>jdbc.properties</include>
                                        
<include>quartz.properties</include>
                                        
<include>sql-map-config.xml</include>
                                    
</includes>
                                    
<filtering>true</filtering>
                                
</resource>
                            
</resources>
                        
</configuration>
                    
</execution>
                
</executions>
            
</plugin>
            
<plugin>
                
<groupId>org.apache.maven.plugins</groupId>
                
<artifactId>maven-jar-plugin</artifactId>
                
<goals>
                    
<goal>jar</goal>
                
</goals>
                
<configuration>
                    
<classesDirectory>bin/</classesDirectory>
                    
<archive>
                        
<manifest>
                            
<mainClass>com.jn.common.Main</mainClass>
                            
<addClasspath>true</addClasspath>
                            
<!-- 追加前缀 -->
                            
<classpathPrefix>lib/</classpathPrefix>
                        
</manifest>
                        
<manifestEntries>
                            
<Class-Path>config/</Class-Path>
                        
</manifestEntries>
                    
</archive>
                    
<excludes>
                        
<exclude>applicationContext.xml</exclude>
                        
<exclude>log4j.xml</exclude>
                        
<exclude>jdbc.properties</exclude>
                        
<exclude>quartz.properties</exclude>
                        
<exclude>sql-map-config.xml</exclude>
                    
</excludes>
                
</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>


本文为原创,欢迎转载,转载请注明出处BlogJava
posted on 2010-09-04 11:37 李 明 阅读(1755) 评论(0)  编辑  收藏 所属分类: Maven2

只有注册用户登录后才能发表评论。


网站导航: