2011-10-13 28 views
5

Trong Eclipse, bạn có thể tạo ra một lọ dự án với các phụ thuộc yêu cầu của nó trong một thư mục con liền kề ...Làm thế nào tôi có thể sử dụng Maven để xây dựng một cái lọ với các thư viện cần thiết trong một thư mục con (như Eclipse)

  1. Xuất> Java-> tập tin JAR Runnable

  2. Chọn Library tùy chọn xử lý: Sao chép thư viện cần thiết vào một thư mục con bên cạnh JAR tạo

có cách nào để làm cái wi này th plugin lắp ráp Maven? Hoặc là có một plugin Maven sẽ thích hợp hơn cho nhiệm vụ này?

Cảm ơn!

Trả lời

4

có bạn có thể sử dụng plugin lắp ráp. pom.xml:

<build> 
    <!-- final name set the jar name, if left it 
    will give defualt "${artifactId}-${version}" --> 
    <finalName>jar final name</finalName> 
    <sourceDirectory>src</sourceDirectory> 

     <!-- compiler plug in --> 
     <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <configuration> 
       <source>1.6</source> 
       <target>1.6</target> 
      </configuration> 
     </plugin> 
     <!-- assembly plugin --> 
     <!-- the assembly plugin is used to define your 
     final deploy output (jar, zip, dir, war, etc..)--> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <executions> 
       <execution> 
        <id>assembly:package</id> 
        <phase>package</phase> 
        <goals> 
         <goal>single</goal> 
        </goals> 
        <configuration> 
         <!-- The filename of the assembled distribution 
         file defualt ${project.build.finalName}--> 
         <finalName>${project.build.finalName}</finalName> 
         <appendAssemblyId>false</appendAssemblyId> 
         <!-- A list of descriptor files path to generate from--> 
         <descriptors> 
          <descriptor>assembly/assembly.xml</descriptor> 
         </descriptors> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
     <!-- jar plug in --> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 
      <version>2.3</version> 
      <configuration> 
       <archive> 
        <manifest> 
         <mainClass>fully.qualified.MainClass</mainClass> 
         <!-- to create a class path to your 
         dependecies you have to fill true in this field--> 
         <addClasspath>true</addClasspath> 
         <!-- if you put your dependencySet/outputDirectory 
         in the assembly file is in a specific folder (lib for example), 
         you need to add classpathPrefix--> 
         <classpathPrefix>lib/</classpathPrefix> 

         <!-- if you defined your dependencySet/outputFileNameMapping 
         in the assembly, instead of using the classpathPrefix, 
         you should use customClasspathLayout, 
         add the classpathPrefix at the begining of the 
         customClasspathLayout, and then add the pattern of the outputFileNameMapping, 
         NOTICE YOU NEED TO ADD '$' BEFOR OF EACH '$'. 
         supported only from version 2.3>--> 
         <!--<classpathLayoutType>custom</classpathLayoutType> 
         <customClasspathLayout> 
          lib/$${artifact.groupId}.$${artifact.artifactId}.$${artifact.extension} 
         </customClasspathLayout>--> 

        </manifest> 

        <manifestEntries> 
         <Class-Path>conf/</Class-Path> 
        </manifestEntries> 
       </archive> 

      </configuration> 
     </plugin> 

    </plugins> 
</build> 

assembly.xml

<?xml version="1.0" encoding="UTF-8"?> 
<assembly> 
    <!--the id will be add to the end of the distribution file --> 
    <id>package</id> 
    <formats> 
     <format>zip</format> 
    </formats> 
    <includeBaseDirectory>true</includeBaseDirectory> 


    <fileSets> 
     <fileSet> 
      <directory>target</directory> 
      <outputDirectory></outputDirectory> 
      <includes> 
       <include>*.jar</include>     
      </includes> 
     </fileSet> 
     <fileSet> 
      <directory>icons</directory> 
      <outputDirectory>icons</outputDirectory> 
      <includes> 
       <include>**/*</include> 
      </includes> 
     </fileSet> 
     <fileSet> 
      <directory>conf</directory> 
      <outputDirectory>conf</outputDirectory> 
      <includes> 
       <include>**/*</include> 
      </includes> 
     </fileSet> 
    </fileSets> 

    <files> 
     <!-- you need to create the bat file yourself --> 
     <file> 
      <source>batFileName.bat</source> 
      <filtered>true</filtered> 
     </file> 
    </files> 

     <dependencySets> 
      <dependencySet> 
       <!--define the outputDirectory of the dependencies, 
        NOTICE: if it's diffrent from '/' make sure to 
        change the classPath configuration for 
        the maven-jar-plugin in the pom--> 
       <outputDirectory>lib</outputDirectory> 
       <!-- maping the dependencies jar names. 
        NOTICE : if you used this definition, you need to use 
        customClasspathLayout classPath configuration 
        for the maven-jar-plugin in the pomg--> 
       <outputFileNameMapping> 
        ${artifact.groupId}.${artifact.artifactId}.${artifact.extension} 
       </outputFileNameMapping> 
       <unpack>false</unpack> 
      </dependencySet> 
     </dependencySets> 

</assembly> 
+0

Có một thời gian khi chúng tôi nói với những người khác sử dụng Maven để có được chỉ với một định nghĩa và rất nhiều "ước" kết quả mà bạn muốn. Với Ant (và các công cụ xây dựng khác nữa) nó sẽ là không có trí tuệ ... :-( – mliebelt

+1

cũng có rất ít lợi ích cho maven, bạn chỉ cần tìm chúng? Quyết định sử dụng maven trong các dự án của chúng ta không phải là của tôi, vì vậy tôi cần phải yêu thích nó và làm cho tốt nhất của tôi –

+1

Cảm ơn bạn đã trả lời chi tiết! Với một vài điều chỉnh, mẫu của bạn hoạt động hoàn hảo cho dự án của chúng tôi. Điều này có nghĩa là chúng tôi có thể di chuyển bản dựng Java đến máy chủ xây dựng. cải thiện rất lớn trong quá trình và sự ổn định của ứng dụng của chúng tôi Một lần nữa, cảm ơn bạn rất nhiều! – HeavyE

1

Không chính xác những gì bạn muốn nhưng nếu bạn sử dụng mục tiêu war:war (với -DfailOnMissingWebXml=false), nó sẽ đặt phụ thuộc vào thư mục WEB-INF/lib trong thư mục target của bạn.

Hoặc xem dependency plugin.

+0

Giống như bạn nói, chứ không phải chính xác những gì chúng tôi đang tìm kiếm nhưng sẽ cho phép chúng tôi tự động xây dựng, vì vậy 1! Nếu tôi không sớm nghe thấy giải pháp toàn Maven, bạn chắc chắn sẽ nhận được tín dụng trả lời. Cảm ơn artbristol! – HeavyE

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