maven的resources插件

maven的resources插件职责是将项目的资源文件拷贝到目标目录。maven将资源划分为两类:main resources 和 test resources。因此有如下三个相应的goal:

1. resources:resources
  • command: mvn resources:resources
  • bound by default to the process-resources life-cycle phase
  • copies the resources for the main source code to the main output directory
  • the resources for your main source code, is by default specified by the <resources> element (project.build.resources, 默认是src/main/resources)
  • the main build output directory, is by default specified by the project.build.outputDirectory element
2. resources:testResources
  • command: mvn resources:testResources
  • bound by default to the process-test-resources life-cycle phase
  • copies the resources for the test source code to the test output directory
  • the resources for your test source code, is by default specified by the <testResources> (project.build.testResources) element
  • the test build output directory, is by default specified by the project.build.testOutputDirectory element
3. resources:copy-resources
  • command: mvn resources:copy-resources
  • requires that you configure the phase to be bound to (specified as part of the plugin configuration)
  • copies resources to an output directory
  • Rather than using the or elements or their defaults, this uses a element that is specified as part of the plugin configuration
  • For more details, see Copy Resources

Some Tips

Specifying resource directories

maven默认src/main/resources下的资源。如果你有资源文件不是存放在这个目录下,那么可以通过<resources>元素指定:

 <project>
      ...
      <build>
           ...
           <resources>
                <resource>
                     <directory>${basedir}/apk</directory>
                     <includes>
                     <include>**/*.apk</include>
                   </includes>
                     <targetPath>META-INF/apk</targetPath>
                </resource>
                <resource>
                     <directory>${basedir}/src/main/resources</directory>
                </resource>
           </resources>
           ...
      </build>
      ...
 </project>

Including and excluding files and directories

除了指定原资源文件和目标目录,我们还可以指定包含或者排除哪些目录或者文件。

例如:

 <project>
      ...
      <build>
           ...
           <resources>
                <resource>
                     <directory>${basedir}/sample</directory>
                     <targetPath>META-INF/sample</targetPath>
                </resource>
                <resource>
                     <directory>${basedir}/docs</directory>
                     <targetPath>META-INF/docs</targetPath>
                </resource>
                <!-- 把源代码打在一起,方便后面SDK打包 -->
                <resource>
                     <directory>${project.build.sourceDirectory}</directory>
                     <includes>
                          <include>com/qq/buy/api/client/**/*.java</include>
                     </includes>
                     <targetPath>META-INF/source</targetPath>
                </resource>
                <resource>
                     <directory>${basedir}/src/main/resources</directory>
                </resource>
           </resources>
      </build>
 </project>     

Resources Filter

最后,我们还可以对资源文件进行变量替换,这个maven称之为resources filter。变量值可以来自 system properties, project properties, filter resources 和 command line.

resources的三个goals都支持resources filter。

例如:

 <project>
      ...
      <build>
           ...
           <resources>
                <resource>
                     <directory>src/main/webapp/WEB-INF</directory>
                     <filtering>true</filtering>
                     <includes>
                          <include>**/*.xml</include>
                     </includes>
                </resource>
                <resource>
                     <directory>src/main/resources</directory>
                     <filtering>false</filtering>
                </resource>
           </resources>
           <filters>
                <filter>config.properties</filter>
           </filters>
      </build>
 </project>     

最后的绝招——the maven antrun plugin

如果resources插件还是不能满足你的需求。那么还有最后一个绝招——maven-antrun-plugin。可以通过在maven中运行ant脚本。

例如,假设有个RPC框架,需要将IDL文件生成java源代码,打包成依赖的jar包。可以这么处理:

 <project>
      ...
      <properties>
           <idl.dir>src/main/idl</idl.dir>
           <idl.package>me.arganzheng.project.api.metadata.idl</idl.package>
           <idl.dist_dir>/usr/local/dist_dir/api/idllib</idl.dist_dir>
      </properties>
      ...
      <profiles>
           <profile>
                <id>production</id>
                <build>
                     <plugins>
                          <plugin>
                               <artifactId>maven-antrun-plugin</artifactId>
                               <executions>
                                    <execution>
                                         <id>generate-idl-protocal-sources</id>
                                         <phase>generate-sources</phase>
                                         <goals>
                                              <goal>run</goal>
                                         </goals>
                                         <configuration>
                                              <tasks>
                                                   <mkdir dir="${project.basedir}/target/idl-temp" />
                                                   <!-- 把IDL java文件编译成class -->
                                                   <javac
                                                        fork="true" executable="${env.JAVA_HOME}/bin/javac"
                                                        srcdir="${idl.dir}"
                                                        encoding="UTF-8"
                                                        destdir="${project.basedir}/target/idl-temp">
                                                        <classpath refid="maven.compile.classpath" />
                                                   </javac>
                                                   <!-- 生成IDL的Request和Response等java文件 -->
                                                   <java classname="com.paipai.platform.tools.AutoGenForCi">
                                                        <classpath refid="maven.compile.classpath" />
                                                        <classpath path="${project.basedir}/target/idl-temp" />
                                                        <arg value="${idl.package}"/>
                                                        <arg path="${project.basedir}/target/idl-temp"/>
                                                        <arg value="app"/>
                                                        <arg value="UTF-8"/>
                                                   </java>
                                              </tasks>
                                         </configuration>
                                    </execution>
                                    <execution>
                                         <id>transfer</id>
                                         <phase>install</phase>
                                         <goals>
                                              <goal>run</goal>
                                         </goals>
                                         <configuration>
                                              <tasks>
                                                   <copy todir="${idl.dist_dir}" file="target/${project.name}-${project.version}.jar" />
                                              </tasks>
                                         </configuration>
                                    </execution>
                               </executions>
                          </plugin>
                          <!-- 把上一步生成的IDL的Request和Response等java文件包含进后面编译时的源码范围 -->
                          <plugin>
                               <groupId>org.codehaus.mojo</groupId>
                               <artifactId>build-helper-maven-plugin</artifactId>
                               <version>1.7</version>
                               <executions>
                                    <execution>
                                         <id>register-idl-protocal-sources</id>
                                         <phase>generate-sources</phase>
                                         <goals>
                                              <goal>add-source</goal>
                                         </goals>
                                         <configuration>
                                              <sources>
                                                   <source>${project.basedir}/target/idl-temp</source>
                                              </sources>
                                         </configuration>
                                    </execution>
                               </executions>
                          </plugin>
                     </plugins>
                </build>
           </profile>
      </profiles>
 </project>

这里放在了profiles中,执行命令的时候需要通过-Pproduction指定。

如果ant脚本比较复杂,可以将其独立成一个文件:

 <project>
      <build>
           <plugins>
                <plugin>
                     <artifactId>maven-antrun-plugin</artifactId>
                     <executions>
                          <execution>
                               <id>transfer</id>
                               <phase>pre-integration-test</phase>
                               <configuration>
                                    <tasks>
                                         <echo>Transfer api war...</echo>
                                         <property name="current_version" value="${project.version}" />
                                         <ant antfile="transfer.xml">
                                              <target name="copyFileCycle" />
                                         </ant>
                                    </tasks>
                               </configuration>
                               <goals>
                                    <goal>run</goal>
                               </goals>
                          </execution>
                     </executions>
                </plugin>
           </plugins>
      </build>
 </project>

transfer.xml脚本就是一个简单的ant脚本:

 <?xml version="1.0" encoding="UTF-8"?>
 <project name="me.arganzheng.project.api" default="copyFileCycle" basedir=".">
      <property name="project.root" value="." />
      <property name="env.default_dist_dir" value="/usr/local/dist_dir" />
      <property name="target.dir" value="${env.default_dist_dir}/api" />
      <target name="copyFileCycle"  description="copy files for cycle">
           <echo message="copy api-metadata war to ${target.dir}"/>
           <mkdir dir="${target.dir}" />
           <copy tofile="${target.dir}/meta.war" file="${project.root}/target/api-sdk-v2-1.0.war" preservelastmodified="true" />
      </target>
 </project>
### 回答1: 这个错误提示可能是因为 Maven 在编译资源文件时出现了问题。可能的原因包括: 1. 资源文件路径不正确或不存在; 2. 资源文件格式不正确; 3. Maven 编译插件版本不兼容。 解决方法包括: 1. 检查资源文件路径是否正确,并确保文件存在; 2. 检查资源文件格式是否正确,例如 XML 文件是否符合规范; 3. 更新 Maven 编译插件版本,或者尝试使用其他插件。 需要更具体的信息才能给出更准确的解决方法。 ### 回答2: "Maven resources compiler" 是一个错误,可能是由于以下原因引起的: 1. Maven 配置错误:在 Maven 的项目配置文件 pom.xml 中的 <build> 标签中可能存在错误的配置。可能需要检查是否正确配置了 resource 目录的路径和编译器插件。 2. 缺失依赖:项目可能缺少必要的插件或依赖。这可能导致 Maven 无法找到或使用正确的编译器插件来编译资源。 3. 资源目录错误:可能没有正确设置 Maven 的资源目录。默认情况下,Maven 所有的资源文件都会存放在项目的 /src/main/resources 目录中,如果目录结构或文件命名不正确,会导致编译错误。 解决这个问题的方法可能有: 1. 检查 Maven 配置:仔细检查项目的 pom.xml 文件,确保正确配置了编译器插件,并指定了正确的资源目录。 2. 确保依赖存在:检查项目是否缺少必要的插件或依赖。可以通过在 pom.xml 文件中添加正确的插件或在 Maven 仓库中查找并添加缺失的依赖来解决这个问题。 3. 检查资源目录:确认资源文件存放在正确的目录中(默认是 /src/main/resources)。检查文件命名和目录结构是否正确,遵循 Maven 的规范。 通过仔细检查并修改 Maven 的配置文件来解决 "Maven resources compiler" 错误。确保依赖、插件和资源目录设置正确,并重新运行 Maven 构建命令(如 mvn compile)以重新编译项目。如仍然出现错误,可以尝试清除 Maven 缓存并重新构建项目。 ### 回答3: "Maven资源编译器错误"是指在使用Maven构建项目时遇到了资源编译器的问题。 一般出现这个错误的原因可能有以下几种情况: 1. Maven配置错误:可能是项目的pom.xml文件中配置不正确或者缺少了一些必要的插件或依赖。我们需要检查pom.xml文件中是否正确配置了资源编译器插件,并确保插件的版本和配置是正确的。 2. 编译器版本问题:有时候我们可能使用了不兼容的编译器版本,导致资源编译失败。可以尝试修改编译器版本,或者查看资源编译器插件的要求,确保使用的编译器版本是支持的。 3. 资源文件路径错误:可能是因为资源文件的路径设置不正确,导致编译器无法找到资源文件。我们需要检查资源文件的路径是否正确,并确保资源文件在正确的位置。 4. 资源文件冲突:有时候项目中可能存在名称相同但内容不同的资源文件,这可能会导致资源编译器无法确定使用哪个文件。我们需要检查项目中的资源文件是否存在冲突,并做出相应的修改。 总之,解决"Maven资源编译器错误"的关键是仔细检查Maven配置、编译器版本、资源文件路径和冲突等因素,确保其正确性和兼容性。如有需要,还可以参考Maven官方文档或搜索相关的解决方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值