如何用maven将maven配置下载最新jar文件打在jar包外

如何用maven将配置文件(*.properties,*.xml)打在jar包外呢? - 开源中国社区
当前访客身份:游客 [
当前位置:
问题是这样的:
当前是一个java的项目,用maven管理的,里面有配置文件放在/src/java/resources/config/*.xml 和/src/java/resources/*.xml 里面,可能以后会有properties的配置文件,现在想要将这个项目打包成jar,同时将依赖拷贝到当前的/target/lib目录下面&忽略src/java/resources/里面的配置文件
这样做的目的在于以后方便更改依赖的jar和配置文件,求指导
&groupId&org.apache.maven.plugins&/groupId&
&artifactId&maven-shade-plugin&/artifactId&
&version&1.2.1&/version&
&executions&
&execution&
&phase&package&/phase&
&goal&shade&/goal&
&configuration&
&transformers&
&transformer
implementation=&org.apache.maven.plugins.shade.resource.ManifestResourceTransformer&&
&mainClass&com.chinaroad.quickstart.Application&/mainClass&
&/transformer&
&/transformers&
&/configuration&
&/execution&
&/executions&
上面的只能讲所有的依赖和config都打包到一个jar中。。。改配置文件和依赖很麻烦,有没有一个好的方式?
共有3个答案
<span class="a_vote_num" id="a_vote_num_
知道一点点,给个类似的例子:
&groupId&org.apache.maven.plugins&/groupId&
&artifactId&maven-dependency-plugin&/artifactId&
&executions&
&execution&
&id&copy-something&/id&
&phase&prepare-package&/phase&
&goal&copy-dependencies&/goal&
&configuration&
&excludeGroupIds& 要排除的groupids &/excludeGroupIds&
&excludeArtifactIds&要排除的artifactIds&/excludeArtifactIds&
&includeTypes&jar&/includeTypes&
&outputDirectory&指定输出的路径&/outputDirectory&
&/configuration&
&/execution&
&/executions&
&/plugins&
&/build& 具体再去看看 dependency-plugin &的用法。
<span class="a_vote_num" id="a_vote_num_
在&resource&里面配置&excludes&
<span class="a_vote_num" id="a_vote_num_
问题已解决,pom如下,最后mvn package -Dmaven.test.skip=true结果在target下面有jar,lib是依赖库,配置文件放在和jar一个目录
&sourceDirectory&src/main/java&/sourceDirectory&
&resources&
&resource&
&directory&src/main/resources&/directory&
&targetPath&${project.build.directory}&/targetPath&
&excludes&
&exclude&**/*.java&/exclude&
&/excludes&
&/resource&
&/resources&
&testSourceDirectory&src/test/java&/testSourceDirectory&
&testResources&
&testResource&
&directory&src/test/resources&/directory&
&filtering&true&/filtering&
&excludes&
&exclude&**/*.java&/exclude&
&/excludes&
&/testResource&
&/testResources&
&pluginManagement&
&groupId&org.eclipse.m2e&/groupId&
&artifactId&lifecycle-mapping&/artifactId&
&version&1.0.0&/version&
&configuration&
&lifecycleMappingMetadata&
&pluginExecutions&
&pluginExecution&
&pluginExecutionFilter&
&groupId&org.apache.maven.plugins&/groupId&
&artifactId&maven-dependency-plugin&/artifactId&
&versionRange&[2.0,)&/versionRange&
&goal&copy-dependencies&/goal&
&/pluginExecutionFilter&
&ignore /&
&/pluginExecution&
&/pluginExecutions&
&/lifecycleMappingMetadata&
&/configuration&
&/plugins&
&/pluginManagement&
&artifactId&maven-compiler-plugin&/artifactId&
&version&2.3.2&/version&
&configuration&
&source&1.6&/source&
&target&1.6&/target&
&encoding&UTF-8&/encoding&
&/configuration&
&!-- 打包源码一起发布到maven仓库中 --&
&artifactId&maven-source-plugin&/artifactId&
&version&2.1&/version&
&configuration&
&attach&true&/attach&
&/configuration&
&executions&
&execution&
&phase&compile&/phase&
&goal&jar&/goal&
&/execution&
&/executions&
&!-- 生成可执行JAR包命令 maven-jar-plugin --&
&groupId&org.apache.maven.plugins&/groupId&
&artifactId&maven-jar-plugin&/artifactId&
&configuration&
&manifest&
&addClasspath&true&/addClasspath&
&classpathPrefix&lib/&/classpathPrefix&
&mainClass&***Main Application *** &/mainClass&
&/manifest&
&manifestEntries&
&Class-Path&./&/Class-Path&
&/manifestEntries&
&/archive&
&/configuration&
&!-- 拷贝依赖的jar包到lib目录 --&
&groupId&org.apache.maven.plugins&/groupId&
&artifactId&maven-dependency-plugin&/artifactId&
&executions&
&execution&
&id&copy&/id&
&phase&package&/phase&
&goal&copy-dependencies&/goal&
&configuration&
&outputDirectory&
${project.build.directory}/lib
&/outputDirectory&
&/configuration&
&/execution&
&/executions&
&!-- 生成可执行JAR包命令 maven-jar-plugin end --&
&!-- 生成可执行JAR包命令 maven-shade-plugin &plugin& &groupId&org.apache.maven.plugins&/groupId&
&artifactId&maven-shade-plugin&/artifactId& &version&1.2.1&/version& &executions&
&execution& &phase&package&/phase& &goals& &goal&shade&/goal& &/goals& &configuration&
&transformers& &transformer implementation=&org.apache.maven.plugins.shade.resource.ManifestResourceTransformer&&
&mainClass&***.Application ***&/mainClass& &/transformer&
&/transformers& &/configuration& &/execution& &/executions& &/plugin& --&
&/plugins&
更多开发者职位上
有什么技术问题吗?
月光小南的其它问题
类似的话题从网上看过许多打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&mons&/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&
&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目录下,最终结构如下:
...generateImportCarSql.jar
&!--&targetPath&${project.build.directory}/config&/targetPath&--&
&/resource&
&/resources&
&!-- 设置编译版本 --&
&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&
&!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 --&
&groupId&org.apache.maven.plugins&/groupId&
&artifactId&maven-jar-plugin&/artifactId&
&configuration&
&!-- 清单文件 --&
&manifest&
&mainClass&com.hdwang.Application&/mainClass&
&addClasspath&true&/addClasspath&
&classpathPrefix&lib/&/classpathPrefix&
&/manifest&
&!-- 给清单文件添加键值对(配置文件外置) --&
&manifestEntries&
&Class-Path&config/&/Class-Path&
&/manifestEntries&
&/archive&
&classesDirectory&
&/classesDirectory&
&/configuration&
&!-- 拷贝依赖的jar包到lib目录 --&
&groupId&org.apache.maven.plugins&/groupId&
&artifactId&maven-dependency-plugin&/artifactId&
&executions&
&execution&
&id&copy&/id&
&phase&package&/phase&
&goal&copy-dependencies&/goal&
&configuration&
&outputDirectory&
${project.build.directory}/lib
&/outputDirectory&
&/configuration&
&/execution&
&/executions&
&!-- 解决资源文件的编码问题 --&
&groupId&org.apache.maven.plugins&/groupId&
&artifactId&maven-resources-plugin&/artifactId&
&version&2.5&/version&
&configuration&
&encoding&UTF-8&/encoding&
&/configuration&
&!-- 打包source文件为jar文件 --&
&artifactId&maven-source-plugin&/artifactId&
&version&2.1&/version&
&configuration&
&attach&true&/attach&
&encoding&UTF-8&/encoding&
&/configuration&
&executions&
&execution&
&phase&compile&/phase&
&goal&jar&/goal&
&/execution&
&/executions&
&/plugins&
&/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包和资源文件了。
阅读(...) 评论()<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&maven把java项目打包,如何把配置文件提出来_百度知道
maven把java项目打包,如何把配置文件提出来
提问者采纳
artifactId}-${&&&nbsp.&${basedir}/target/classes&/plugin&&
&version&configuration&&
&true&&&${&&&target&${&/mainClass&4;
&&&&&/configuration&maven-compiler-plugin&plugin&${basedir}/resources&&&&/version&used&groupId&&
&&/&&&&nbsp://
&&&&&&&&nbsp://&&/execution&&/goal&&/version&directory&/resources&
&/Class-Path&gt你好;project
&&&groupId&manifestEntries&&&
&lt.6&&/plugin&&goal&&&&&&&
&&&" target="_blank">
&lt.jna&&&&nbsp?&&&&&&phase
&&&&/parent&&&&manifest&&execution&&/encoding&/goals&&&/configuration&/executions&finalName&&&copy-dependencies&/goal&classpathPrefix&&&&/phase&&&&&&source&/addClasspath&&nbsp.w3;&&nbsp.plugins&&&&&nbsp.artifactId&&&&package&package&addClasspath&&/manifest&&nbsp./id&gt.2&&&&&
&&&/executions&&dependencies&gt.&&nbsp.sourceEncoding& &&/groupId&filtering&&/goals&& &id&/modelVersion&gt。&goals&&&&
&&groupId&
&&&&&plugin&executions&&
&UTF-8&&name&gt.org/POM/4;
&lt.xml&/classpathPrefix&&&&appendAssemblyId&
&lt.xsd& &&artifactId&&nbsp.0& &&&nbsp://&&artifactId&gt.0&&/descriptors&gt!--&
&/goal&/url&&&&&&
&artifactId&&
&&&&&&install&/artifactId&&&&/groupId&&executions&
&/artifactId&maven-jar-plugin&&goal&&nbsp.0;plugin&&& &&addClasspath&&
&&nbsp.&version&classpathPrefix&&this&&resource&/goals&/execution&/artifactId&--&&&exclude&&version=&&&;&
&&nbsp.0;&excludes&&&&
&&nbsp:xsi=&id&gt.&&/addClasspath&gt.org/POM/4;&;&
&package&&nbsp.tmtpt&&
&lt.&&&&&&&&&&&&/id&&descriptor&--&&&/properties&build&&
&&/groupId&gt.org/2001/XMLSchema-instance&&
&&&nbsp.0;&&&nbsp.conf&&&goals&&&&;&&mainClass&&&
&&&nbsp.xsd&&&&&&&
&&&nbsp:///configuration&
&/classesDirectory&groupId&to&&&
&&&&version&&nbsp://phase&&jar&&/artifactId&&bind&&&&nbsp.version}&lt.&&goal&&&&
&&archive&
&&&&&&&/name&
&/artifactId&&&&&&
&&lt.4&&&execution&finalName&&tmrpt&" target="_blank">&
&copy-resources&lt.0;/resources&gt.directory}/maven-archiver/resources&lt://
&&maven-dependency-plugin&&&&manifest&&1;&/defaultGoal&
&&&&&<a href="&&phase&&nbsp.2&1;modelVersion&&execution&&&phase&
&&&&nbsp.w3;&&nbsp.&outputDirectory&/execution&
&&&&&${basedir}/src/
&&&&&encoding&&&1;&&nbsp.0;&nbsp?xml&&&
&lt.&&&&&com://
&&&&&nbsp.EtlClientStart&&<a href=";&&&&&id&
&&/finalName&&&/configuration&&&/outputDirectory&/directory&&configuration&&nbsp:schemaLocation=&&
&&nbsp:xsi=&&&&/artifactId&&/dependency&&&artifactId&&nbsp.org/POM/4;&&&&&&&&inheritance&&&&&
${project.directory}/maven-archiver/lib
&&&&/exclude&&&jna&&&nbsp.0;
&&/descriptor&&&&&&&tmrpt-client&&&&&&&--&&/groupId&&
&lt.0&&nbsp!--&nbsp.0&
&lt.apache.plugins&
&lt.0;&/configuration&&&&&artifactId&&2;&nbsp://&&&&/manifestEntries&
&/finalName& &&package&maven-resources-plugin&&&/phase&maven-assembly-plugin&&
&&&/target&/dependencies&&&version&
&lt.sourceEncoding&&nbsp./plugin&/id&&&&&&&&/plugin&
&&&true&&/appendAssemblyId&&&vpetl-client&&/groupId&/outputDirectory&&nbsp.plugins&lt.&&nbsp.plugins&&true&&lt.archive&& &goals&&&&plugin&&nbsp.3;outputDirectory&&&&&
&defaultGoal&
&/executions&&copy-resources&&true&artifactId& &/resource&
&UTF-8&&*;&&nbsp.0;&${basedir}/target/maven-archiver&&&outputDirectory&
&lt.0;&&encoding&&/groupId&encoding&executions&&&&&plugins&&configuration&&&lt.maven:这个把配置文件提取出来可以再&&/artifactId&
&&&goal&&/plugins&/goals&gt.org/xsd/maven-4;&&
&lt.&&&&&parent&&0;
&single&&&&groupId&
&dependency&/configuration&&&&& &&configuration&plugin&&&
&&&&&mainClass&copy-dependencies&/excludes&/build&
&/filtering&/project&&&&&&nbsp。举例如下;&&&
&&&nbsp.&&&nbsp.xml里面配置java项目文件路径下文件打包方式来实现;&&nbsp
java工程师
其他类似问题
为您推荐:
maven的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁出处:http://www.kafeitu.me/ 引言
配置文件几乎是每个项目中不可或缺的文件类型之一,常用的配置文件类型有xml、properties等,配置文件的好处不言而喻,利用配置文件可以灵活地设置软件的运行参数,并且可以在更改配置文件后在不重启应用的情况下即时生效。
写这篇文章的原因是最近在一个项目中引入了我对配置文件的管理方式,我觉得有必要与大家分享,希望能抛砖引玉。
1. 我所知道的配置文件管理方式
下面大概列出了几类对配置文件的管理方式,请对号入座
1.1 配置文件是什么?
除非应用不需要配置文件(例如Hello World),否则请无视,
1.2 数据库方式
把配置文件保存在数据库,看起来这种方式很不错,配置不会丢失方便管理,其实问题很大;举个简单的例子,在A模块需要调用B模块的服务,访问B模块的URL配置在数据库,首先需要在A中读取B模块的URL,然后再发送请求,问题紧随而来,如果这个功能只有一个开发人员负责还好,假如多个人都要调试那就麻烦了,因为配置保存在数据库(整个Team使用同一个数据库测试),每个开发人员的B模块的访问端口(Web容器的端口)又不相同或者应用的ContextPath不同,要想顺利调试就要更改数据库的B模块URL值,这样多个开发人员就发生了冲突;问题很严重!
1.3 XML方式
对于使用SSH或者其他的框架、插件的应用在src/main/resources下面肯定有不少的xml配置文件,今天的主题是应用级的配置管理,所以暂且抛开框架必须的XML配置文件,先来看看下面的XML配置文件内容。
这种方式在之前很受欢迎,也是系统属性的主要配置方式,不过使用起来总归不太简洁、灵活(不要和我争论XML与Properties)。
1.4 属性文件方式
下面是kft-activiti-demo中
application.properties文件的一部分:
sql.type=h2
jdbc.driver=org.h2.Driver
jdbc.url=jdbc:h2:file:~/kft-activiti-AUTO_SERVER=TRUE
jdbc.username=sa
jdbc.password=
system.version=${project.version}
activiti.version=${activiti.version}
export.diagram.path=${export.diagram.path}
diagram.http.url=${diagram.http.url}
相比而言属性文件方式比XML方式要简洁一些,不用严格的XML标签包装即可设置属性的名称,对于多级配置可以用点号(.)分割的方式。
2. 分析我的管理方式
我在几个项目中所采用的是第4中方式,也就是
属性文件的方式来管理应用的配置,读取属性文件可以利用Java的Properties对象,或者借助Spring的
2.1 利用Maven资源过滤设置属性值
简单来说就是利用Maven的Resource Filter功能,pom.xml中的build配置如下:
src/main/resources
这样在编译时
src/main/resources目录下面中文件()只要有
${foo}占位符就可以自动替换成实际的值了,例如1.4节中属性
system.version使用的是一个占位符而非实际的值,${project.version}表示pom.xml文件中的
project标签的
export.diagram.path、
diagram.http.url也是使用了占位符的方式,很明显这两个属性的值不是pom.xml文件可以提供,所以如果要动态设置值可以通过在pom.xml中添加的方式,或者把放在profile中,如此可以根据不同的环境(开发、UAT、生产)动态设置不同的值。
当然也可以在编译时通过给JVM传递参数的方式,例如:
mvm clean compile -Dexport.diagram.path=/var/kft/diagrams
编译完成后查看target/classes/application.properties文件的内容,属性
export.diagram.path的值被正确替换了:
export.diagram.path=/var/kft/diagrams
2.2 读取配置文件
读取配置文件可以直接里面Java的Properties读取,下面的代码简单实现了读取属性集合:
Properties props = new Properties();
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource(location);
InputStream is = resource.getInputStream();
propertiesPersister.load(props, new InputStreamReader(is, &UTF-8&));
如果在把读取的属性集合保存在一个静态Map对象中就可以在任何可以执行Java代码的地方获取应用的属性了,工具类PropertiesFileUtil简单实现了属性缓存功能:
public class PropertyFileUtil {
private static P
public void loadProperties(String location) {
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource(location);
InputStream is = resource.getInputStream();
PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
propertiesPersister.load(properties, new InputStreamReader(is, &UTF-8&));
// 获取属性值
public static String get(String key) {
String propertyValue = properties.getProperty(key);
return propertyV
抛出一个问题:属性文件中定义了属性的值和平台有关,团队中的成员使用的平台有Window、Linux、Mac,对于这样的情况目前只能修改
application.properties文件,但是不能把更改提交到SCM上否则会影响其他人的使用……目前没有好办法,稍后给出解决办法。
2.2 多配置文件重载功能
2.1节中简单的PropertyFileUtil工具只能做到读取一个配置文件,这对于一些多余一个子系统来说就不太能满足需求了。
对于一个项目拆分的多个子系统来说如果每个子系统都配置一套属性集合最后就会出现一个问题——配置重复,修改起来也会比较麻烦;解决的办法很简单——把公共的属性抽取出来单独保存在一个公共的属性文件中,我喜欢命名为:
<mon.properties。
这个公共的属性文件可以用来保存一些数据库连接、公共目录、子模块的URL等信息,如此一来子系统中的
application.properties中只需要设置当前子系统需要的属性即可,在读取属性文件时可以依次读取多个(
先读取mon.properties,
再读取application.properties),这样最终获取的属性集合就是两个文件的并集。
在刚刚的PropertyFileUtil类中添加一个
loadProperties方法,接收的参数是一个可变数组,循环读取属性文件。
* 载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的载入.
* 文件路径使用Spring Resource格式, 文件编码使用UTF-8.
* @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
public static Properties loadProperties(String... resourcesPaths) throws IOException {
Properties props = new Properties();
for (String location : resourcesPaths) {
Resource resource = resourceLoader.getResource(location);
InputStream is = resource.getInputStream();
propertiesPersister.load(props, new InputStreamReader(is, DEFAULT_ENCODING));
有了这个方法我们可以这样调用这个工具类:
PropertyFileUtil.loadProperties(&mon.properties&, &application.properties&);
2.1中抛出的问题也迎刃而解了,把配置文件再根据类型划分:
<mon.properties
application.properties 各个子系统的属性
application.
local.properties 本地属性(用于开发时)
请注意:不要把application.local.properties纳入到版本控制(SCM)中,这个文件只能在本地开发环境出现!!!
最后读取的顺序应该这样写:
PropertyFileUtil.loadProperties(&mon.properties&, &application.properties&, &application.local.properties&);
2.3 根据环境不同选择不同的配置文件
2.2的多文件重载解决了
2.1中的问题,但是现在又遇到一个新问题:如何根据不同的环境读取不同的属性文件。
开发时最后读取
application.local.properties
测试时最后读取
application.test.properties
生产环境最后读取
/etc/foo/application.properties
这么做的目的在于每一个环境的配置都不相同,第一步读取公共配置,第二步读取子系统的属性,最后读取不同环境使用的特殊配置,这样才能做到最完美、最灵活。
既然属性的值可以通过国占位符的方式替换,我们也可以顺藤摸瓜把读取文件的顺序也管理起来,所以又引入了一个属性文件:
application-file.properties;它的配置如下:
mon.properties
B=application.properties
C=${env.prop.application.file}
env.prop.application.file的值可以动态指定,可以利用Maven的Profile功能实现,例如针对开发环境配置一个ID为
dev的profile并设置默认激活状态;对于部署到测试、生产环境可以在打包时添加-Ptest或者-Pproduct参数使用不同的Profile;关键在于每一个profile中配置的env.prop.application.file值不同,例如:
classpath*:/application.local.properties
file:/etc/foo/application.properties
而对于生产环境来说可以把
env.spring.application.file改为
/etc/foo/application.properties。
public class PropertyFileUtil {
private static Logger logger = LoggerFactory.getLogger(PropertyFileUtil.class);
private static P
private static PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
private static ResourceLoader resourceLoader = new DefaultResourceLoader();
private static final String DEFAULT_ENCODING = &UTF-8&;
* 初始化读取配置文件,读取的文件列表位于classpath下面的application-files.properties&br/&
* 多个配置文件会用最后面的覆盖相同属性值
* @throws IOException
读取属性文件时
public static void init() throws IOException {
String fileNames = &application-files.properties&;
innerInit(fileNames);
* 初始化读取配置文件,读取的文件列表位于classpath下面的application-[type]-files.properties&br/&
* 多个配置文件会用最后面的覆盖相同属性值
* @param type 配置文件类型,application-[type]-files.properties
* @throws IOException
读取属性文件时
public static void init(String type) throws IOException {
String fileNames = &application-& + type + &-files.properties&;
innerInit(fileNames);
* 内部处理
* @param fileNames
* @throws IOException
private static void innerInit(String fileNames) throws IOException {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream resourceAsStream = loader.getResourceAsStream(fileNames);
// 默认的Properties实现使用HashMap算法,为了保持原有顺序使用有序Map
Properties files = new LinkedProperties();
files.load(resourceAsStream);
Set&Object& fileKeySet = files.keySet();
String[] propFiles = new String[fileKeySet.size()];
List&Object& fileList = new ArrayList&Object&();
fileList.addAll(files.keySet());
for (int i = 0; i & propFiles. i++) {
String fileKey = fileList.get(i).toString();
propFiles[i] = files.getProperty(fileKey);
logger.debug(&读取属性文件:{}&, ArrayUtils.toString(propFiles));;
properties = loadProperties(propFiles);
Set&Object& keySet = properties.keySet();
for (Object key : keySet) {
logger.debug(&property: {}, value: {}&, key, properties.getProperty(key.toString()));
默认的Properties类使用的是Hash算法故无序,为了保持多个配置文件的读取顺序与约定的一致
所以需要一个自定义的有序Properties实现,参加:
2.4 启动载入与动态重载
为了能让其他类读取到属性需要有一个地方统一管理属性的读取、重载,我们可以创建一个Servlet来处理这件事情,在Servlet的
init()方法中读取属性(调用PropertiesFileUtil.init()方法),可以根据请求参数的
action值的不同做出不同的处理。
我们把这个Servlet命名为
PropertiesServlet,映射路径为:/properties-servlet。
import java.io.IOE
import java.util.S
import javax.servlet.S
import javax.servlet.ServletC
import javax.servlet.ServletC
import javax.servlet.ServletE
import javax.servlet.http.HttpS
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletR
import me.kafeitu.demo.activiti.util.PropertyFileU
import mons.lang3.StringU
import org.slf4j.L
import org.slf4j.LoggerF
* classpath下面的属性配置文件读取初始化类
public class PropertiesServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected Logger logger = LoggerFactory.getLogger(getClass());
* @see Servlet#init(ServletConfig)
public void init(ServletConfig config) throws ServletException {
PropertyFileUtil.init();
ServletContext servletContext = config.getServletContext();
setParameterToServerContext(servletContext);;
(&++++ 初始化[classpath下面的属性配置文件]完成 ++++&);
} catch (IOException e) {
logger.error(&初始化classpath下的属性文件失败&, e);
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = StringUtils.defaultString(req.getParameter(&action&));
resp.setContentType(&text/charset=UTF-8&);
if (&reload&.equals(action)) { // 重载
PropertyFileUtil.init();
setParameterToServerContext(req.getSession().getServletContext());
(&++++ 重新初始化[classpath下面的属性配置文件]完成 ++++,{IP={}}&, req.getRemoteAddr());
resp.getWriter().print(&
属性文件重载成功!
writeProperties(resp);
} catch (IOException e) {
logger.error(&重新初始化classpath下的属性文件失败&, e);
} else if (&getprop&.equals(action)) { // 获取属性
String key = StringUtils.defaultString(req.getParameter(&key&));
resp.getWriter().print(key + &=& + PropertyFileUtil.get(key));
} else if (&list&.equals(action)) { // 获取属性列表
writeProperties(resp);
private void setParameterToServerContext(ServletContext servletContext) {
servletContext.setAttribute(&prop&, PropertyFileUtil.getKeyValueMap());
Servlet发布之后就可以动态管理配置了,例如发布到生产环境后如果有配置需要更改(编辑服务器上保存的配置文件)可以访问下面的路径重载配置:
/appname/properties-servlet?action=reload
2.5 让人凌乱的占位符配置
下面的log4j代码来自实际项目,看一看${…}的数量,有点小恐怖了,对于大型项目配置文件会更多,占位符也会更多。
3. 配置文件辅助Maven插件--portable-config-maven-plugin
是《Maven实战》作者
刚刚发布的一款插件,这个插件的用途就是在打包时根据不同环境替换原有配置,这个插件独特的地方在于不用使用占位符预先定义在配置文件中,而是直接替换的方式覆盖原有配置。
该插件支持替换properties、xml格式的配置文件,使用方法也很简单,在pom.xml中添加插件的定义:
com.juvenxu.portable-config-maven-plugin
portable-config-maven-plugin
replace-package
src/main/portable/test.xml
src/main/portable/test.xml文件的内容就是需要替换的属性集合,下面列出了properties和xml的不同配置,xml替换使用XPATH规则。
awesome app
当然你可以定义几个不同环境的profile来决定使用哪个替换规则,在打包(mvn package)时该插件会被激活执行替换动作。
有了这款插件对于一些默认的属性可以不使用占位符定义,取而代之的是实际的配置,所以对现有的配置无任何影响,又可以灵活的更改配置。
转载自:相关 [maven 文件 管理] 推荐:
配置文件几乎是每个项目中不可或缺的文件类型之一,常用的配置文件类型有xml、properties等,配置文件的好处不言而喻,利用配置文件可以灵活地设置软件的运行参数,并且可以在更改配置文件后在不重启应用的情况下即时生效. 写这篇文章的原因是最近在一个项目中引入了我对配置文件的管理方式,我觉得有必要与大家分享,希望能抛砖引玉.
- 行业应用 - ITeye博客
下面具一个如何把edtftpj.jar 包上传到本地maven仓库的例子. 首先有edtftpj.jar 的jar文件,我把它放到了 D:/ (D盘根目录),. - 进入“命令行提示符”,进入到D:/,然后执行mvn install:install-file -Dfile=edtftpj.jar -DgroupId=com.enterprisedt -DartifactId=edtFTPj -Dversion=2.0.4 -Dpackaging=jar.
- ITeye博客
在上一篇博文中讲到了maven的配置,今天要讲maven与eclipse的集成. 现在在java开发中,eclipse占据了绝大部分的市场份额. 其他的关于NetBeans,IDEA中的关于maven的配置,读者可参阅相关资料. (注:关于IDEA,据说是款非常强大的集成开发环境,但是据说对svn的支持不是很好,道听途说而已).
- CSDN博客架构设计推荐文章
博客地址:/?p=142. 由于工作主要是前端开发,做后端的项目比较少,最近自己做个项目,发觉好多的都忘了,这里写篇博客整理下maven工程下整合spring+mybatis+freemarker相关内容. 新建个Archetype为maven-archetype-webapp的maven项目(安装maven插件:http://download.eclipse.org/technology/m2e/releases),在pom文件添加相关依赖:.
- 企业架构 - ITeye博客
从Maven仓库中导出jar包:进入工程pom.xml 所在的目录下,. 输入以下命令:mvn dependency:copy-dependencies -DoutputDirectory=lib. 更简单的
mvn dependency:copy-dependencies
会导出到target\dependency 下面.
- 企业架构 - ITeye博客
“分天下为三十六郡,郡置守,尉,监” —— 《史记·秦始皇本纪》. 所有用Maven管理的真实的项目都应该是分模块的,每个模块都对应着一个pom.xml. 它们之间通过继承和聚合(也称作多模块,multi- module)相互关联. 我们明明在开发一个项目,划分模块后,导入Eclipse变成了N个项目,这会带来复杂度,给开发带来 不便.
- 开源软件 - ITeye博客
例如上篇helloworld的pom.xml配置文件中的. 其实就是坐标,那么Maven的坐标元素包括groupId、artifactId、version、packaging、classifier. 当一个项目通过依赖,插件或者父项目引用与另外一个项目关联的时候,Maven通过坐标来精确定位一个项目.
- 研发管理 - ITeye博客
简单的Maven模块结构是这样的:. 上述简单示意图中,有一个父项目(app-parent)聚合很多子项目(app-util, app-dao, app-service, app-web). 每个项目,不管是父子,都含有一个pom.xml文件. 而且要注意的是,小括号中标出了每个项目的打包类型. 父项目是pom,也只能是pom.
- 互联网 - ITeye博客
在进行项目部署时,需要将maven项目所依赖的jar导出到指定目录,本文讲解如何导出项目依赖的jar包. 一、导出到默认目录 targed/dependency .
从Maven项目中导出项目依赖的jar包:进入工程pom.xml 所在的目录下,执行如下命令:.
或在eclipse中,选择项目的pom.xml文件,点击右键菜单中的Run As,见下图红框中,在弹出的Configuration窗口中,输入
dependency:copy-dependencies 后,点击运行.
- 加州旅客 - LinuxTOY
如果您喜好 OS X
Finder 风格的文件管理器,Marlin 必然适合您. Marlin 文件管理器作为 Elementary OS 项目的一部分由 ammonkey 开发. Marlin 拥有如同 Finder 的列表视图:. 以及小小的选择 + 号和单击启动模式:. 现在 Marlin 测试版本的 PPA 仓库已经上线,在 Ubuntu 11.04 中安装,请进行:.
坚持分享优质有趣的原创文章,并保留作者信息和版权声明,任何问题请联系:@。

我要回帖

更多关于 maven配置下载最新jar 的文章

 

随机推荐