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

发布于 2021-11-19 03:28:38 字数 1159 浏览 388 评论 5

问题是这样的:

当前是一个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中。。。改配置文件和依赖很麻烦,有没有一个好的方式?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终止放荡 2021-11-23 17:52:49

正解

草莓味的萝莉 2021-11-23 17:52:46

这个好用!

孤檠 2021-11-23 17:38:15
问题已解决,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>

水水月牙 2021-11-23 17:15:10

在<resource>里面配置<excludes>

http://maven.apache.org/pom.html#Resources

百思不得你姐 2021-11-23 14:06:24

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

<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  的用法。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文