2011-10-13 8 views
5

W środowisku Eclipse można utworzyć słoiczek projektu z wymaganymi zależnościami w sąsiednim podfolderze, wykonując ...Jak używać Mavena do budowania słoika z wymaganymi bibliotekami w podfolderze (takim jak Eclipse)?

  1. Export-> Java-> plik Runnable JAR

  2. opcja obsługi Wybierz Biblioteka: Skopiuj wymagane biblioteki w podkatalogu obok wygenerowanym JAR

Czy istnieje sposób, aby zrobić to wi th the assembly montażu Maven? Czy jest jeszcze inna wtyczka Maven, która byłaby bardziej odpowiednia dla tego zadania?

Dzięki!

Odpowiedz

4

Tak, możesz użyć wtyczki zespołu. 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

Były czasy, kiedy mówiliśmy innym, aby używali Mavena, aby uzyskać tylko definicję i wiele "konwencji", czego pragniesz. Z Ant (i innymi narzędziami do budowania) byłoby to nieobliczalne ... :-( – mliebelt

+1

cóż, jest kilka korzyści dla maven, wystarczy je znaleźć? Decyzja o użyciu maven w naszych projektach nie jest moje, więc muszę go kochać i robić to, co najlepsze –

+1

Dziękuję za szczegółową odpowiedź! Za pomocą kilku poprawek, twoje sample działają idealnie dla naszego projektu, co oznacza, że ​​możemy przenieść kompilację Java do serwera kompilacji. Ogromna poprawa w naszym procesie i stabilność aplikacji Ponownie, dziękuję bardzo! – HeavyE

1

To nie jest dokładnie to, co chcesz, ale jeśli używasz cel war:war (z -DfailOnMissingWebXml=false) będzie można umieścić w zależności katalogu WEB-INF/lib w folderze target.

Można również sprawdzić numer dependency plugin.

+0

Tak jak powiedziałeś, nie jest to dokładnie to, czego szukasz, ale pozwoli nam zautomatyzować budować, więc +1! Jeśli wkrótce nie usłyszę rozwiązania typu all-Maven, na pewno uzyskasz kredyt na odpowiedź. Dzięki artBristol! – HeavyE

Powiązane problemy