maven可以管理加载配置文件吗|怎么设置dubbo的xml配置让maven加载进去

A. maven把java项目打包,如何把配置文件提出来

你好:这个把配置文件提取出来可以再pm.xml里面配置java项目文件路径下文件打包方式来实现。举例如下,参考下。

<?xmlversion="1.0"?><project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.tmtpt</groupId> <artifactId>tmrpt</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>tmrpt-client</artifactId> <version>2.2.2</version> <name>vpetl-client</name> <url>www.tmrpt.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.sun.jna</groupId> <artifactId>jna</artifactId> <version>4.0.0</version> </dependency> </dependencies> <build> <finalName>tmrpt-client</finalName> <defaultGoal>install</defaultGoal> <plugins> <!–<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version><configuration><source>1.6</source><target>1.6</target> <encoding>UTF-8</encoding></configuration></plugin>–> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.vprisk.main.EtlClientStart</mainClass> </manifest> </archive> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classesDirectory>${basedir}/target/classes</classesDirectory> <finalName>${project.artifactId}-${project.version}</finalName> <outputDirectory>${basedir}/target/maven-archiver</outputDirectory> <excludes> <exclude>*.conf</exclude> </excludes> </configuration> </execution> </executions> </plugin> <!–拷贝依赖的jar包到lib目录–> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>-dependencies</id> <phase>package</phase> <goals> <goal>-dependencies</goal> </goals> <configuration> <outputDirectory> ${project.build.directory}/maven-archiver/lib </outputDirectory> </configuration> </execution> </executions> </plugin> <plugin><artifactId>maven-resources-plugin</artifactId><executions><execution><id>-resources</id><phase>package</phase><goals><goal>-resources</goal></goals><configuration><outputDirectory>${project.build.directory}/maven-archiver/resources</outputDirectory><resources><resource><directory>${basedir}/resources</directory><filtering>true</filtering></resource></resources></configuration></execution></executions></plugin> <!–<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <encoding>UTF-8</encoding> <appendAssemblyId>true</appendAssemblyId> <descriptors> <descriptor>${basedir}/src/assembly.xml</descriptor> </descriptors> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix/> <mainClass>com.vprisk.main.EtlserverStart</mainClass> </manifest> <manifestEntries> <Class-Path>.</Class-Path> </manifestEntries> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase>bindtothepackagingphase <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>–> </plugins> </build></project>

B. 如何用maven将配置文件打在jar包外

<build><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><plugins><plugin><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><goals><goal>-dependencies</goal></goals></pluginExecutionFilter><action><ignore/></action></pluginExecution></pluginExecutions></lifecycleMappingMetadata></configuration></plugin></plugins></pluginManagement><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.6</source><target>1.6</target><encoding>UTF-8</encoding></configuration></plugin><!–打包源码一起发布到maven仓库中–><plugin><artifactId>maven-source-plugin</artifactId><version>2.1</version><configuration><attach>true</attach></configuration><executions><execution><phase>compile</phase><goals><goal>jar</goal></goals></execution></executions></plugin><!–生成可执行JAR包命令maven-jar-plugin–><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><configuration><archive><manifest><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix><mainClass>***MainApplication***</mainClass></manifest><manifestEntries><Class-Path>./</Class-Path></manifestEntries></archive></configuration></plugin><!–拷贝依赖的jar包到lib目录–><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><executions><execution><id></id><phase>package</phase><goals><goal>-dependencies</goal></goals><configuration><outputDirectory>${project.build.directory}/lib</outputDirectory></configuration></execution></executions></plugin><!–生成可执行JAR包命令maven-jar-pluginend–><!–生成可执行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><transformerimplementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>***.Application***</mainClass></transformer></transformers></configuration></execution></executions></plugin>–></plugins></build>

可以参考这个配置,使用maven package将在target下面生成jar包,lib是依赖库,配置文件放在和jar一个目录

C. 怎么设置bbo的xml配置让maven加载进去

现在很流行的Dubbo很多朋友都听说过吧,最近我也在看这方面的东西,分享先我的心得笔记。先说说我们团队要做的项目框架,很简单重在实现基于zookeeper的bbo注册。框架:springmvc+spring+zookeeper+bbo项目分三层,model存放数据,view页面展示、controller下面具体逻辑实现。通过bbo消费方和供应方注册,供应方给消费方暴露接口,供消费方调用。 工程部署需要配置文件有: applicationContext-bbo.xml {– <– 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 –> <– 使用zookeeper注册中心暴露服务地址 –> <– 生成远程服务代理,可以像使用本地bean一样使用demoService –> <bbo:reference id="demoService" interface="com.unj.bbotest.provider.DemoService" /> –} bbo.properties {– <–基于ZooKeeper的Dubbo注册中心直接部署tomcat,修改WEB-INF下文件–> bbo.registry.address=zookeeper://127.0.0.1:2181 bbo.admin.root.password=root bbo.admin.guest.password=guest –} zoo_sample.cfg {– zookeeper/conf/下,修改zoo_sample.cfg为zoo.cfg,启动bin/下zkServer.cmd –}因为引入bbo,摒弃了原有Web Service项目的wdls暴露,由于项目依赖关系严重,项目使用maven构建,通过Maven pom.xml三维坐标引入jar包,调用bbo暴露接口开发。性能测试工具:LoadRunner、jmeter接口测试工具:LoadRunner、jmeter、soapUI、Spotlight安全测试工具:NStalker-Web、AppScan、TamperIESetup自动化工具 :BadboyInstaller、QTP /** * @author wonter * <b>描述:</b> 一天学一个模式 更新中,请关注我的博客!<br> * <b>博客:</b> http://www.cnblogs.com/javame <br> * <b>邮件:</b> [email protected] <br>

D. maven打包问题,如何打包配置文件

方法一:pom.xml文件抄配置:如果配置文件放在src/main/resources目录下,maven默认会把这个文件夹下的文件复制到classes目录下,如果你不是放在默认目录下,你可以手动指定Resources目录和输出目录。配置如下:

<build><sourceDirectory>src/</sourceDirectory><outputDirectory>build/</outputDirectory></build>

方法二:把配置文件打包到其他目录:可以使用org.apache.maven.plugins插件。

E. maven的src/test/resources中的配置文件怎样读取

解决myeclipse部署maven时,src/main/resources里面配置文件加载不到webapp下classes路径下的问题。有时候是src/main/resources下面版的,有时候是src/main/java下面的。把没有编译的文件,权先点击:Excluded,然后再点击:Remove,便可。 查看更多答案>>

F. maven是配置文件吗

你好,Maven软件可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具。在JavaEE中,我们可以使用Maven方便地管理团队合作的项目。

G. Maven-Spring多项目加载配置文件

1,classpath:applicationContext.xml改为classpath*:applicationContext.xml这么写也是一样的效果:classpath*:/applicationContext.xml2,在applicationContext.xml引用其它的配置文件:如果在同一个专jar包这么写,

Java代码

<importresource="classpath:/applicationContext-action.xml"/>

要导入其它jar包中属的配置文件,这么写

Java代码

<importresource="classpath*:/applicationContext-service.xml"/>

就这么简单,重新打包、部署、启动成功。

H. maven怎么配置才可以自动加载插件

下载maven的bin,在apache官方网站可以下载。下载下来之后,解压,找个路径放进去, 把bin的位置设在环境变量里,新建环境变量MAVEN_HOME在PATH里加入maven的bin的路径由于Maven依赖Java运行环境,因此使用Maven之前需要配置Java的运行环境。下载并安装JDK,配置JDK的环境变量JAVA_HOME,否则maven将无法使用配置完毕后,在Windows命令提示符下,输入mvn -v测试一下,配置成功显示如图:配置成功后开始在Eclipse中配置Maven,点击eclipse菜单栏Help->Eclipse Marketplace搜索关键字maven到插件Maven Integration for Eclipse 并点击安装即可,如下图:安装完毕后,点击重启eclipse重启后,为了使得Eclipse中安装的Maven插件,同windows中安装的那个相同,需要让eclipse中的maven重新定位一下,点击Window -> Preference -> Maven -> Installation -> Add进行设置

I. JAVA maven创建web项目,把Spring框架配置文件放在src/main/resources中读取不到配置文件

classpath:是从类路径里查找配置文件,也就是/WEB-INF/classes目录下找SpringMVC-servlet.xml。你写了classpath了,不会从web-info下找,而是去web-inf/classes下面找,所以找不到。

J. maven配置文件settings.xml中的profiles怎么用

profile介绍

4.1profile简介

profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。比如说,我们可以通过profile定义在jdk1.5以上使用一套配置信息,在jdk1.5以下使用另外一套配置信息;或者有时候我们可以通过操作系统的不同来使用不同的配置信息,比如windows下是一套信息,linux下又是另外一套信息,等等。具体的激活条件有哪些我在后文会讲到。

4.2profile的定义位置

对于使用Maven3,我们可以有多个地方定义profile。定义的地方不同,它的作用范围也不同。

针对于特定项目的profile配置我们可以定义在该项目的pom.xml中。

针对于特定用户的profile配置,我们可以在用户的settings.xml文件中定义profile。该文件在用户家目录下的“.m2”目录下。

全局的profile配置。全局的profile是定义在Maven安装目录下的“conf/settings.xml”文件中的。

4.3profile中能定义的信息

profile中能够定义的配置信息跟profile所处的位置是相关的。以下就分两种情况来讨论,一种是定义在settings.xml中,另一种是定义在pom.xml中。

4.3.1 profile定义在settings.xml中

当profile定义在settings.xml中时意味着该profile是全局的,它会对所有项目或者某一用户的所有项目都产生作用。因为它是全局的,所以在settings.xml中只能定义一些相对而言范围宽泛一点的配置信息,比如远程仓库等。而一些比较细致一点的需要根据项目的不同来定义的就需要定义在项目的pom.xml中。具体而言,能够定义在settings.xml中的信息有<repositories>、<pluginRepositories>和<properties>。定义在<properties>里面的键值对可以在pom.xml中使用。

4.3.2 profile定义在pom.xml中

定义在pom.xml中的profile可以定义更多的信息。主要有以下这些:

l<repositories>

l<pluginRepositories>

l<dependencies>

l<plugins>

l<properties>

l<dependencyManagement>

l<distributionManagement>

l还有build元素下面的子元素,主要包括:

<defaultGoal>

<resources>

<testResources>

<finalName>

4.4profile的激活方式

Maven给我们提供了多种不同的profile激活方式。比如我们可以使用-P参数显示的激活一个profile,也可以根据环境条件的设置让它自动激活等。下面将对它们一一进行介绍:

4.4.1 使用activeByDefault设置激活

先看下面一个配置

Xml代码

<profiles>

<profile>

<id>profileTest1</id>

<properties>

<hello>world</hello>

</properties>

<activation>

<activeByDefault>true</activeByDefault>

</activation>

</profile>

<profile>

<id>profileTest2</id>

<properties>

<hello>andy</hello>

</properties>

</profile>

</profiles>

我们可以在profile中的activation元素中指定激活条件,当没有指定条件,然后指定activeByDefault为true的时候就表示当没有指定其他profile为激活状态时,该profile就默认会被激活。所以当我们调用mvn package的时候上面的profileTest1将会被激活,但是当我们使用mvn package –P profileTest2的时候将激活profileTest2,而这个时候profileTest1将不会被激活。

4.4.2 在settings.xml中使用activeProfiles指定处于激活状态的profile

我们可以在settings.xml中使用activeProfiles来指定需要激活的profile,这种方式激活的profile将所有情况下都处于激活状态。比如现在我们定义了如下两个profile

Xml代码

<profiles>

<profile>

<id>profileTest1</id>

<properties>

<hello>world</hello>

</properties>

</profile>

<profile>

<id>profileTest2</id>

<properties>

<hello>andy</hello>

</properties>

</profile>

</profiles>

这里的profile可以是定义在settings.xml中的,也可以是定义在pom.xml中的。这个时候如果我们需要指定profileTest1为激活状态,那么我们就可以在settings.xml中定义activeProfiles,具体定义如下:

Xml代码

<activeProfiles>

<activeProfile>profileTest1</activeProfile>

</activeProfiles>

考虑这样一种情况,我们在activeProfiles下同时定义了多个需要激活的profile。这里还拿上面的profile定义来举例,我们定义了同时激活profileTest1和profileTest2。

Xml代码

<activeProfiles>

<activeProfile>profileTest1</activeProfile>

<activeProfile>profileTest2</activeProfile>

</activeProfiles>

从profileTest1和profileTest2我们可以看出它们共同定义了属性hello。那么这个时候我在pom.xml中使用属性hello的时候,它到底取的哪个值呢?是根据activeProfile定义的顺序,后面的覆盖前面的吗?根据我的测试,答案是非也,它是根据profile定义的先后顺序来进行覆盖取值的,然后后面定义的会覆盖前面定义的。

4.4.3 使用-P参数显示的激活一个profile

假设我们现在有如下定义的profiles

Xml代码

<profiles>

<profile>

<id>profileTest1</id>

<properties>

<hello>world</hello>

</properties>

</profile>

<profile>

<id>profileTest2</id>

<properties>

<hello>andy</hello>

</properties>

</profile>

<profiles>

那么当我们在进行Maven操作时就可以使用-P参数显示的指定当前激活的是哪一个profile了。比如我们需要在对项目进行打包的时候使用id为profileTest1的profile,我们就可以这样做:

Cmd代码

mvnpackage–PprofileTest1

当我们使用activeByDefault或settings.xml中定义了处于激活的profile,但是当我们在进行某些操作的时候又不想它处于激活状态,这个时候我们可以这样做:

Cmd代码

Mvnpackage–P!profileTest1

这里假设profileTest1是在settings.xml中使用activeProfile标记的处于激活状态的profile,那么当我们使用“-P !profile”的时候就表示在当前操作中该profile将不处于激活状态。

4.4.4根据环境来激活profile

profile一个非常重要的特性就是它可以根据不同的环境来激活,比如说根据操作系统的不同激活不同的profile,也可以根据jdk版本的不同激活不同的profile,等等。

4.4.4.1根据jdk来激活profile

Xml代码

<profiles>

<profile>

<id>profileTest1</id>

<jdk>1.5</jdk>

</profile>

<profiles>

上面情况表示在jdk为1.5版本系列的时候激活profileTest1。

Xml代码

<profiles>

<profile>

<id>profileTest1</id>

<jdk>[1.4,1.7)</jdk>

</profile>

<profiles>

上面的情况表示在jdk为1.4、1.5和1.6的时候激活profileTest1。

4.4.4.2根据操作系统来激活profile

Xml代码

<profiles>

<profile>

<id>profileTest1</id>

<activation>

<os>

<name>WindowsXP</name>

<family>Windows</family>

<arch>x86</arch>

<version>5.1.2600</version>

</os>

</activation>

</profile>

</profiles>

上面的情况就是根据操作系统的类型来激活profileTest1。

4.4.4.3根据系统属性来激活profile

Xml代码

<profiles>

<profile>

<id>profileTest1</id>

<activation>

<property>

<name>hello</name>

<value>world</value>

</property>

</activation>

</profile>

</profiles>

上面的profileTest1将在提供了系统属性hello,并且其值为world的时候激活。下面的做法可以激活profileTest1。

Cmd代码

mvnpackage–Dhello=world

当是下面的这种定义形式时,profileTest1将在指定了系统属性hello,且其值为任意值的时候被激活。

Xml代码

<profiles>

<profile>

<id>profileTest1</id>

<activation>

<property>

<name>hello</name>

</property>

</activation>

</profile>

</profiles>

4.4.4.4根据文件是否存在激活profile

Xml代码

<profiles>

<profile>

<id>profileTest1</id>

<activation>

<file>

<exists>target</exists>

</file>

</activation>

</profile>

</profiles>

上面的定义表示当存在target文件时激活profileTest1。

Xml代码

<profiles>

<profile>

<id>profileTest1</id>

<activation>

<file>

<missing>target</missing>

</file>

</activation>

</profile>

</profiles>

上面的定义表示当不存在target文件时激活profileTest1。

4.5查看当前处于激活状态的profile

我们可以同时定义多个profile,那么在建立项目的过程中,到底激活的是哪一个profile呢?Maven为我们提供了一个指令可以查看当前处于激活状态的profile都有哪些,这个指定就是mvn help:active-profiles。

现在假设我们的settings.xml文件中有如下profile的定义:

<profiles>

<profile>

<id>profileTest1</id>

<activation>

<file>

<missing>target</missing>

</file>

</activation>

</profile>

</profiles>

<activeProfiles>

<activeProfile>profileTest1</activeProfile>

</activeProfiles>

这个时候我们可以看到,我们已经定义了profileTest1始终为激活状态,这个时候我们使用mvn help:active-profiles查看处于激活状态的profile时,就会打印出如下内容:


赞 (0)