2013-05-04 38 views
18

Tôi có phiên bản khác nhau nhỏ của the question mà tôi đã tạo gần đây. Tôi có dự án Maven dưới Netbeans 7.3, không có bất kỳ tệp nào build.xml để định cấu hình tùy chọn tòa nhà, trong khi có pom.xml mà tôi sử dụng để nhập các thư viện khác. Bây giờ, tôi có một tệp văn bản (giả sử textfile.txt) được lưu trữ trong thư mục dự án trong Netbeans 7.3, ví dụ:Di chuyển tệp văn bản vào thư mục đích khi biên dịch dự án Maven

project folder 
    textfile.txt 
    src 
    package 
    package.subpackage 
     MyClass.java 

Khi tôi biên soạn tôi nhận được một thư mục target nơi jar tập tin được đưa vào, ví dụ

project folder 
    textfile.txt 
    target 
    classes 
    generated-sources 
    ....etc 
    test-classes 
    MyProject.jar 
    src 
    package 
    package.subpackage 
     MyClass.java 

Làm cách nào để tạo tệp textfile.txt trong thư mục đích khi tôi biên dịch dự án Maven?

Trả lời

28

Cách thứ nhất là đặt các tệp vào src/main/resources là thư mục dành cho lưu trữ tài nguyên tuân thủ, tức là các tài nguyên được đưa vào tệp jar (ví dụ: hình ảnh cho biểu tượng).

Nếu bạn cần làm cho tệp cấu hình được phân phối bằng bình, nhưng được ngăn cách bởi nó, bạn phải chỉnh sửa tệp pom.xml. Câu trả lời có thể là thêm plugin sau giữa các thẻ <plugins></plugins>.

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.8</version> 
    <executions> 
     <execution> 
      <phase>test</phase> 
      <goals> 
       <goal>run</goal> 
      </goals> 
      <configuration> 
       <tasks> 
        <echo>Using env.test.properties</echo> 
        <copy file="textfile.txt" tofile="${basedir}/target/textfile.txt"/> 
        </tasks> 
       </configuration> 
      </execution> 
     </executions> 
    </plugin> 

Hơn nữa, như bạn có thể đọc here bạn cũng có thể nhập tất cả các nguồn lực từ một thư mục "đầu vào" để một thư mục "đầu ra" bên trong mục tiêu bằng cách sử dụng các plugin chuyên dụng, ví dụ:

<plugin> 
    <artifactId>maven-resources-plugin</artifactId> 
    <version>3.0.1</version> 
    <executions> 
     <execution> 
      <id>copy-resources</id> 
      <!-- here the phase you need --> 
      <phase>validate</phase> 
      <goals> 
       <goal>copy-resources</goal> 
      </goals> 
      <configuration> 
       <outputDirectory>${basedir}/target/output</outputDirectory> 
       <resources>   
        <resource> 
         <directory>input</directory> 
         <filtering>true</filtering> 
        </resource> 
       </resources>    
      </configuration>    
     </execution> 
    </executions> 
</plugin> 
+0

Cảm ơn bạn! Tôi không chắc chắn rằng plugin đầu tiên được sử dụng đúng, nhưng hoạt động tốt. –

+0

Tuyệt vời, điều này giúp ích rất nhiều, cảm ơn JeanValjean! –

11

cách đơn giản nhất, sử dụng một số tài nguyên như tôi biết (biết thêm chi tiết về cấu hình tài nguyên mà bạn có thể tìm thấy ở đây: https://maven.apache.org/plugins/maven-resources-plugin/):

<build> 
    <plugins> 
    <!-- your plugins, including or not maven-resource-plugin --> 
    </plugins> 
    <resources> 
    <resource> 
     <filtering>true</filtering><!-- if it is neccessary --> 
     <directory>${project.basedir}</directory><!-- from --> 
     <targetPath>${project.build.directory}</targetPath><!-- to --> 
     <includes><!-- what --> 
     <include>textfile.txt</include> 
     </includes> 
    </resource> 
    </resources> 
</build> 
+0

Làm thế nào để đảm bảo rằng textfile.txt cũng được sao chép vào tệp jar? – Sam

+0

tôi hiểu rồi. Cảm ơn bạn. – Sam

4

để có một Absol ute kiểm soát đầu ra của xây dựng của bạn, bạn có thể sử dụng "Apache Maven hội Plugin". Bạn có thể định nghĩa khá nhiều thứ (định dạng, thư mục con ...).

Plugin hội cho Maven chủ yếu nhằm cho phép người dùng tổng hợp đầu ra của dự án cùng với phụ thuộc, mô-đun, tài liệu trang web và các tệp khác vào một bản lưu trữ phân phối duy nhất.

More info

Bạn phải cài đặt plugin trong file pom của bạn:

<plugins> 
    <plugin> 
     <artifactId>maven-assembly-plugin</artifactId> 
     <configuration> 
      <descriptors> 
       <descriptor>src/main/assembly/yourassembly.xml</descriptor> 
      </descriptors> 
     </configuration> 
    </plugin> 
</plugins> 

<pluginManagement> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <version>x.x</version> 
      <configuration> 
       <descriptors> 
        <descriptor>src/main/assembly/yourassembly.xml</descriptor> 
       </descriptors> 
      </configuration> 
      <executions> 
       <execution> 
        <id>make-assembly</id> <!-- this is used for inheritance merges --> 
        <phase>package</phase> <!-- append to the packaging phase. --> 
        <goals> 
         <goal>single</goal> <!-- goals == mojos --> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</pluginManagement> 

Trong trường hợp bạn mô tả "yourassembly.xml" sẽ là như sau:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd"> 
    <id>yourassembly</id> 
    <formats> 
    <format>tar.gz</format> 
    <format>dir</format> 
    </formats> 
    <fileSets> 

    <fileSet> 
     <directory>${project.basedir}</directory> 
     <includes> 
     <include>README*</include> 
     <include>LICENSE*</include> 
     <include>NOTICE*</include> 
     </includes> 
     <useDefaultExcludes>true</useDefaultExcludes> 
    </fileSet> 

    <fileSet> 
     <directory>${project.build.directory}</directory> 
     <outputDirectory>/</outputDirectory> 
     <includes> 
     <include>*.jar</include> 
     </includes> 
    </fileSet> 

    <fileSet> 
     <directory>${basedir}</directory> 
     <outputDirectory>/</outputDirectory> 
     <includes> 
     <include>textfile.txt</include> 
     </includes> 
    </fileSet> 

    <dependencySets> 
     <dependencySet> 
      <outputDirectory>lib</outputDirectory> 
      <unpack>false</unpack> 
     </dependencySet> 
    </dependencySets> 

    </fileSets> 

</assembly> 
Các vấn đề liên quan