2010-08-11 96 views
13

我想将一些资源添加到jar文件中。我“分析”它并将它们添加到构建部分。未放入jar文件的maven资源

但资源不在最终的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> 

在这里,我发出命令:

有什么不对?

回答

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

@帕斯卡尔:非常好的答案。我有和你一样的pom.xml文件,它不会在jar文件中包含正确的文件。我会稍后用更新版本的maven2(我的是2.0.9)来尝试。谢谢你的时间! – ssedano 2010-08-12 11:36:41

+0

@Udo我的确在使用Maven 2.2.1 – 2010-08-12 13:04:23

+0

@Pascal和amra:现在我开始工作了,谢谢!但如果我想将文件放在根文件夹(jar)中? targetPath> target/myProject does not work – ssedano 2010-08-13 14:00:37

5

在maven中可以用两种方式指定资源。简单的只是复制到目标/类目录的目录。您可以只配置resource directories,filteringincluded/excluded files。默认资源目录是src/main/resources

对于复杂的设置,您需要配置maven插件。当您运行安装following actions run。在包装阶段运行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> 

可以省略包括一部分,如果型材/ MyFolder中仅包含的文件包括。

+0

谢谢你的时间,正如我对帕斯卡尔说的,我会尝试升级maven,我会发布结果。谢谢。 – ssedano 2010-08-12 11:37:18

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> 
2

问题是:<targetPath>.</targetPath>,启动它解决问题。

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