标签:
从网上看过许多打jar包的例子,大多是将配置文件打进jar包的。经过本人一番研究,终于搞清楚了怎样将jar包的配置文件外置。
废话不说,直接上spring的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.hdwang</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>4.0.9.RELEASE</spring.version> </properties> <dependencies> <!-- 通用包 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!-- poi类库 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.13</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.13</version> </dependency> <!-- mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.18</version> </dependency> <!-- spring包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!-- mybatis包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.3</version> </dependency> <!-- dbcp --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <!-- 拼音 --> <dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency> </dependencies> <build> <finalName>generateImportCarSql</finalName> <sourceDirectory>src/main/java</sourceDirectory> <resources> <!-- 控制资源文件的拷贝 --> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.tld</include> </includes> <!--<filtering>false</filtering>--> <!-- 配置文件外置(在打包时取消注释) 资源文件拷贝到编译输出路径target的子文件夹config目录下,最终结构如下: target ...config ...spring ...mapper ...env ...lib ...generateImportCarSql.jar --> <!--<targetPath>${project.build.directory}/config</targetPath>--> </resource> </resources> <plugins> <!-- 设置编译版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <!-- 清单文件 --> <manifest> <mainClass>com.hdwang.Application</mainClass> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> <!-- 给清单文件添加键值对(配置文件外置) --> <manifestEntries> <Class-Path>config/</Class-Path> </manifestEntries> </archive> <classesDirectory> </classesDirectory> </configuration> </plugin> <!-- 拷贝依赖的jar包到lib目录 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory> ${project.build.directory}/lib </outputDirectory> </configuration> </execution> </executions> </plugin> <!-- 解决资源文件的编码问题 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 打包source文件为jar文件 --> <plugin> <artifactId>maven-source-plugin</artifactId> <version>2.1</version> <configuration> <attach>true</attach> <encoding>UTF-8</encoding> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
使用配置文件外置打包时,注意取消targetPath配置节的注释(使用IDE开发时,运行项目报错需要注释掉,注意配置文件输出目录不能是classes,否则会将配置文件打进jar包),最终生成的目录结构如下(运行所需文件及文件夹):
maven打包命令:mvn package
运行命令: java -jar generateImportCarSql.jar
注意:本项目是spring普通项目,非spring web项目,配置文件由spring加载,spring配置文件路径采用classpath写法,下面展示各种spring路径配置实例:
1.java代码启动spring项目路径写法
2.spring配置文件引用另一个spring配置文件
3.spring配置文件中placeHolder路径填写
4.spring继承mybatis时mybatis配置文件路径填写
由于以上各种路径的配置均采用相对于classpath路径的写法。故在项目打包后和项目运行时均可以运行,关键在于classpath路径的设定。
附上jar包清单文件的内容,注意class-path的路径可以配置多个文件夹,多个jar包,之间用空格隔开,注意文件夹一定要在结尾加上/,例如:classpath为当前目录写 ./ 为当前目前下级config文件夹则为 ./config/ 或者config/
掌握了class-path路径的配置,你就可以随意放置你引用的第三方jar包和资源文件了。
使用maven给spring项目打可直接运行的jar包(配置文件内置外置的打法)
标签:
原文地址:http://www.cnblogs.com/hdwang/p/5418747.html