maven资源未放在jar文件中

sse*_*ano 14 resources maven-2

我想在jar文件中添加一些资源.我"分析"它并将它们添加到构建部分.

但资源不在最终的jar文件中.

这是我的个人资料部分pom.xml:

<profile>
  <id>myProfile</id>
  <build>
    <finalName>name</finalName>
    <resources>
      <resource>
        <targetPath>.</targetPath>
        <filtering>false</filtering>
        <directory>${basedir}/profiles/myFolder</directory>
        <includes>
          <include>file.txt</include>
          <include>file.xml</include>
        </includes>
      </resource>
    </resources>
  </build>
</profile>
Run Code Online (Sandbox Code Playgroud)

这里是我发出的命令:

mvn clean install -PmyProfile
Run Code Online (Sandbox Code Playgroud)

怎么了?

Pas*_*ent 11

您的POM代码片看起来很全面,我无法重现您的问题.这是我使用的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow</groupId>
  <artifactId>Q3459013</artifactId>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <profiles>
    <profile>
      <id>myProfile</id>
      <build>
        <finalName>name</finalName>
        <resources>
          <resource>
            <directory>${basedir}/profiles/myFolder</directory>
            <filtering>false</filtering>
            <includes>
              <include>file.txt</include>
              <include>file.xml</include>
            </includes>
          </resource>
        </resources>
      </build>
    </profile>
  </profiles>
</project>
Run Code Online (Sandbox Code Playgroud)

具有以下项目结构:

$ tree .
.
??? pom.xml
??? profiles
?   ??? myFolder
?       ??? file.txt
?       ??? file.xml
??? src
    ??? main
    ?   ??? java
    ?       ??? com
    ?           ??? stackoverflow
    ?               ??? App.java
    ??? test
        ??? java
            ??? com
                ??? stackoverflow
                    ??? AppTest.java

11 directories, 5 files

这就是我得到的:

$ mvn clean install -PmyProfile
...
$ cd target
$ tree .
.
??? classes
?   ??? com
?   ?   ??? stackoverflow
?   ?       ??? App.class
?   ??? file.txt
?   ??? file.xml
??? maven-archiver
?   ??? pom.properties
??? name.jar
??? surefire-reports
?   ??? com.stackoverflow.AppTest.txt
?   ??? TEST-com.stackoverflow.AppTest.xml
??? test-classes
    ??? com
        ??? stackoverflow
            ??? AppTest.class
$ jar xvf name.jar 
  created: META-INF/
 inflated: META-INF/MANIFEST.MF
  created: com/
  created: com/stackoverflow/
 inflated: file.xml
 inflated: com/stackoverflow/App.class
 inflated: file.txt
  created: META-INF/maven/
  created: META-INF/maven/com.stackoverflow/
  created: META-INF/maven/com.stackoverflow/Q3459013/
 inflated: META-INF/maven/com.stackoverflow/Q3459013/pom.xml
 inflated: META-INF/maven/com.stackoverflow/Q3459013/pom.properties

按预期工作.

  • @Udo我不确定我是否理解这个问题,但在`package`期间,Maven会查看`target/classes`的内容.如果你把东西放在`target/classes`之外,它就不会在jar中了.而不是使用`targetPath`,我建议在`resource`目录下创建适当的目录结构. (2认同)

amr*_*mra 5

可以在maven中以两种方式指定资源.简单的只是复制到目标/类目录的目录.您只需配置资源目录,过滤包含/排除的文件.默认资源目录是src/main/resources.

对于复杂的设置,您需要配置maven插件.运行安装 后运行以下操作.在打包阶段,使用资源运行maven-resources-plugin :资源目标.

对于配置,仅使用资源mojo参数.见例子.

将您的个人资料移至$ {projectdir}/profiles/myFolder.

<resources>
    <resource>
         <directory>profiles/myFolder</directory>
         <includes>
             <include>file.txt</include>
             <include>file.xml</include>
         </includes>
    </resource>
</resources>
Run Code Online (Sandbox Code Playgroud)

如果profiles/myFolder仅包含要包含的文件,则可以省略包含部分.