maven资源未放在jar文件中

时间:2010-08-11 13:53:17

标签: maven-2 resources

我想在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>

这里是我发出的命令:

mvn clean install -PmyProfile

怎么了?

4 个答案:

答案 0 :(得分: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>

使用以下项目结构:

$ 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

按预期工作。

答案 1 :(得分:5)

可以在maven中以两种方式指定资源。简单的只是复制到 target / classes 目录的目录。您只需配置resource directoriesfilteringincluded/excluded files即可。默认资源目录是 src / main / resources

对于复杂设置,您需要配置maven插件。当您运行 install following actions run时。在打包阶段,使用 resources:resources 目标运行 maven-resources-plugin

对于配置,仅使用resources mojo parameters。请参阅example

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

<resources>
    <resource>
         <directory>profiles/myFolder</directory>
         <includes>
             <include>file.txt</include>
             <include>file.xml</include>
         </includes>
    </resource>
</resources>

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

答案 2 :(得分:3)

尝试这种简单的结构,这对我有用。

<profile>
      <id>myProfile</id>
      <build>
        <finalName>name</finalName>
        <resources>
          <resource>
                <targetPath>webapp</targetPath>
                <directory>src/main/webapp</directory>
          </resource>
          <resource>
                <directory>src/main/resources</directory>
          </resource>
        </resources>
      </build>
    </profile>

答案 3 :(得分:2)

问题是:<targetPath>.</targetPath>,开始解决问题。

打包jar maven使用target / classes文件夹的内容,否则使用maven-jar-plugin并配置self自己要复制的内容。