开发者社区> 问答> 正文

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

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

展开
收起
kun坤 2020-06-03 10:05:15 483 0
1 条回答
写回答
取消 提交回答
  • 知道一点点,给个类似的例子:

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

    ###### 在<resource>里面配置<excludes>
    http://maven.apache.org/pom.html#Resources ######

    问题已解决,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>

    ######这个好用!######正解

    2020-06-03 10:05:23
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
使用Spring.Initializr定制工程脚手架 立即下载
陈曦:使用Spring.Initializr定制工程脚手架 立即下载
低代码开发师(初级)实战教程 立即下载