
pom.xml文件配置:如果配置文件放在src/main/resources目录下,maven默认会把这个文件夹下的文件复制到classes目录下,如果不是放在默认目录下,可以手动指定Resources目录和输出目录。配置如下:
<build>
<finalName>WEBAPP</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
1、The source plugin can be used to create a jar file of the project sources from the command line or by binding the goal to the project's build lifecycle. To generate the jar from the command line, use the following command:运行后会在target目录中找到生成的源文件jar包。2、在pom.xml中添加:<build<plugins<plugin<artifactIdmaven-source-plugin</artifactId<version2.1</version<configuration<attachtrue</attach</configuration<executions<execution<phasecompile</phase<goals<goaljar</goal</goals</execution</executions</plugins</build配置中指定了phase为compile,意思是在生命周期compile的时候就将源文件打包,即只要执行的mvn命令包括compile这一阶段,就会将源代码打包。同样,phase还可以指定为package、install等等。(1)默认 Spring Boot 项目结构,资源文件放置在 src/main/resources 中,测试的资源文件在 src/test/resources 中。
src/main/resources 与 src/test/resources 的区别:
(2)将项目打包后,解压 jar 可以发现原先 src/main/resources 目录下的资源文件已经被打包进来了:
(3)但有时我们的资源文件并不一定是放在 src/main/resources 目录下,比如我的项目通常会将资源文件放在 src/test/resources 目录下:
原因:根据实践经验表明,测试完后的配置项是最完整的,且经常会忘记替换正式版参数,因而选择将配置文件全部放置在 src/test/resources 目录下。
(4)又比如 mybatis 的 mapper.xml 文件,我们习惯把它和 Mapper.java 放一起
(5)但上面这两种情况的资源文件,在使用 maven 打包时是不会被打包进 jar 的。这时候我们便要指定需要打包的资源文件,这个有如下两种方法可以实现。
(1)<resources>标签位于 <build>标签内,用于指定项目资源文件的位置。比如下面配置我们指定了 src/test/resources 也是资源文件目录:
(2)而对于写在包下的 Mapper.xml 文件,我们则可以通过如下配置指明资源文件位置:
提示:其中 * / 这样的写法,是为了保证各级子目录下的资源文件被打包。
(3)我们还可以通过 excludes 标签剔除不需要的资源:
(1)除了使用 <resources>标签外,我们也可以使用 maven-resources-plugin 插件实现同样的目的。比如下面配置把 src/test/resources 目录下的资源文件打包到 classes 目录下:
(2)而对于写在包下的 Mapper.xml 文件,我们也可以通过 maven-resources-plugin 插件将其打包到相应位置:
(3)使用 maven-resources-plugin 插件时,我们同样可以通过 excludes 标签剔除不需要的资源:
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)