如何用maven将配置文件(*.properties,*.xml)打在jar包外呢?

月光小南 发布于 2013/11/20 11:33
阅读 28K+
收藏 1

问题是这样的:

当前是一个java的项目,用maven管理的,里面有配置文件放在/src/java/resources/config/*.xml 和/src/java/resources/*.xml 里面,可能以后会有properties的配置文件,现在想要将这个项目打包成jar,同时将依赖拷贝到当前的/target/lib目录下面&忽略src/java/resources/里面的配置文件

这样做的目的在于以后方便更改依赖的jar和配置文件,求指导

<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>com.chinaroad.quickstart.Application</mainClass>
								</transformer>
							</transformers>
						</configuration>
					</execution>
				</executions>
			</plugin>


上面的只能讲所有的依赖和config都打包到一个jar中。。。改配置文件和依赖很麻烦,有没有一个好的方式?

加载中
0
子木007
子木007

知道一点点,给个类似的例子:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-something</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <excludeGroupIds> 要排除的groupids </excludeGroupIds>
                        <excludeArtifactIds>要排除的artifactIds</excludeArtifactIds>
                        <includeTypes>jar</includeTypes>
                        <outputDirectory>指定输出的路径</outputDirectory>
                    </configuration>
                </execution>
            </executions>        
        </plugin>
    </plugins>        
</build>
具体再去看看 dependency-plugin  的用法。




0
0
月光小南
月光小南
问题已解决,pom如下,最后mvn package -Dmaven.test.skip=true结果在target下面有jar,lib是依赖库,配置文件放在和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>copy-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>***Main Application *** </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>copy</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>
								${project.build.directory}/lib
							</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<!-- 生成可执行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>
	</build>

秀小川
秀小川
这个好用!
纳川思源
正解
返回顶部
顶部