2010-08-11 40 views
13

Tôi muốn thêm vào tệp jar một số tài nguyên. Tôi "profiled" nó và thêm chúng vào phần xây dựng.tài nguyên maven không được đặt trong tệp jar

Nhưng các tài nguyên không có trong tệp jar cuối cùng.

Ở đây nó đi phần Tiểu sử của pom.xml tôi:

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

Và đây lệnh tôi phát hành:

Có chuyện gì vậy?

Trả lời

11

Đoạn mã POM của bạn trông đẹp trên toàn cầu và tôi không thể tái tạo sự cố của bạn. Đây là pom.xml tôi đã sử dụng:

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

Với cấu trúc dự án sau:

 
$ 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 

Và đây là những gì tôi nhận được:

 
$ 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 

trình như mong đợi.

+1

@Pascal: câu trả lời rất hay. Tôi có pom.xml giống như bạn giữ nó không bao gồm trong tệp jar các tệp thích hợp. Tôi sẽ thử sau với một phiên bản mới hơn của maven2 (tôi là 2.0.9). Cảm ơn bạn đã dành thời gian cho bạn! – ssedano

+0

@Udo Tôi thực sự đang sử dụng Maven 2.2.1 –

+0

@Pascal và amra: Bây giờ tôi đã có nó để làm việc cảm ơn bạn! nhưng nếu tôi muốn đặt các tập tin trong thư mục gốc (jar)? targetPath> target/myProject không hoạt động – ssedano

5

Tài nguyên có thể được chỉ định theo hai cách trong maven. Đơn giản chỉ là thư mục được sao chép vào thư mục đích/lớp học . Bạn chỉ có thể định cấu hình resource directories, filteringincluded/excluded files. Thư mục tài nguyên mặc định là src/main/resources.

Đối với cài đặt phức tạp, bạn cần định cấu hình plugin maven. Khi bạn chạy , hãy cài đặtfollowing actions run. Trong giai đoạn đóng gói chạy maven-resources-plugin với tài nguyên: tài nguyên mục tiêu.

Đối với cấu hình chỉ sử dụng resources mojo parameters. Xem example.

Di chuyển hồ sơ của bạn đến $ {projectdir}/profiles/myFolder.

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

Bạn có thể bỏ qua bao gồm một phần nếu hồ sơ/Myfolder chỉ chứa các file bao gồm.

+0

Cảm ơn bạn đã dành thời gian, như tôi đã nói với Pascal, tôi sẽ thử nâng cấp maven và tôi sẽ đăng kết quả. Cảm ơn bạn. – ssedano

3

Hãy thử loại cấu trúc đơn giản này, điều này phù hợp với tôi.

<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

Sự cố là: <targetPath>.</targetPath>, khởi động để khắc phục sự cố.

Để gói maven jar sử dụng nội dung của thư mục đích/lớp, nếu không hãy sử dụng maven-jar-plugin và tự cấu hình để sao chép.

Các vấn đề liên quan