最近由於項目需要,研究了一下maven的打包,項目要做到
1,生成3個目錄/lib,/conf,/bin目錄
2,把所有的jar目錄編譯、拷貝到/lib目錄(包括maven的jar包和lib目錄下的jar,以及編譯的jar包)
3,把所有的啟動腳本從工程根目錄拷貝到/bin目錄
4,把所有的配置文件從src/main/resources拷貝到/conf
下面是配置的pom.xml,我把相關的配置都加了注釋,一看就能明白,把build節點拷貝到你們的項目中,就基本可以用了:)
<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>test</groupId> <artifactId>test.common</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>test.common</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- 這裡省略n行 --> </dependencies> <build> <sourceDirectory>src/main/java</sourceDirectory> <resources> <!-- 把src/main/resources目錄下所有的文件拷貝到conf目錄中 --> <resource> <directory>src/main/resources</directory> <targetPath>${project.build.directory}/conf</targetPath> </resource> <!-- 把lib目錄下所有的文件拷貝到lib目錄中 (可能有些jar包沒有辦法在maven中找到,需要放在lib目錄中) --> <resource> <directory>lib</directory> <targetPath>${project.build.directory}/lib</targetPath> </resource> <!-- 把放在根目錄下的腳本文件.sh,.bat拷貝到bin目錄中 --> <resource> <directory>.</directory> <includes> <include>**/*.sh</include> <include>**/*.bat</include> </includes> <targetPath>${project.build.directory}/bin</targetPath> </resource> </resources> <plugins> <!-- 用於編譯的plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <fork>true</fork> <defaultLibBundleDir>lib</defaultLibBundleDir> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> <!-- 如果配置了JAVA_HOME,下面應該可以不用配 --> <executable>C:\Program Files (x86)\Java\jdk1.8.0_91\bin\javac.exe</executable> </configuration> </plugin> <!-- 用於生成jar包的plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <!-- 把生成的jar包放在lib目錄下(和其他所有jar包一起) --> <outputDirectory>${project.build.directory}/lib</outputDirectory> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> </archive> <excludes> <!-- 排除掉一些文件,不要放到jar包中, 這裡是為了排除掉src/main/resources中的文件(它們應該放到conf目錄) 這裡只能指定要排除的目標文件,而不能指定源文件,雖然不夠完美,但是基本能達到目的。 --> <exclude>*.xml</exclude> <exclude>*.properties</exclude> </excludes> </configuration> </plugin> <!-- 用於拷貝maven依賴的plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.10</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <!-- 把依賴的所有maven jar包拷貝到lib目錄中(這樣所有的jar包都在lib目錄中) --> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> <!-- 用於拷貝resource的plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 配置生成源代碼jar的plugin --> <plugin> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <configuration> <attach>true</attach> <encoding>UTF-8</encoding> <!-- 配置源代碼jar文件的存放路徑,和其他jar文件一起放在lib目錄 --> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
然後執行maven package打包。如果是eclipse,在Project Explorer-> Run As –> Maven Build… –>在Goals中輸入package,點Run進行編譯。
在bin目錄下的啟動腳本start.bat可以這麼寫:
@ECHO OFF set CLASSPATH=%JAVA_HOME%/lib set JAVA=%JAVA_HOME%/bin/java set CLASSPATH=%CLASSPATH%;../conf set JAVA_OPTIONS=-Djava.ext.dirs="../lib" "%JAVA%" -Xms512m -Xmx1024m -classpath "%CLASSPATH%" %JAVA_OPTIONS% test.HangqingEntrance
以上:
把conf目錄添加到CLASSPATH中,因為conf目錄保存的是從src/main/resources拷貝來的配置文件。
設置-Djava.ext.dirs="../lib" 。因為jar包都放在lib目錄。