2011-10-13 13 views
5

En Eclipse, puede crear un jar de proyecto con sus dependencias requeridas en una subcarpeta adyacente haciendo ...¿Cómo puedo usar Maven para construir un jar con las bibliotecas requeridas en una subcarpeta (como Eclipse)

  1. Exportar-> Java> archivo JAR Ejecutable

  2. la configuración futura Biblioteca Selecta: Copiar bibliotecas necesarias en una sub-carpeta situado junto al JAR generado

¿hay una manera de hacerlo esto wi ¿El plugin Maven assembly? ¿O hay otro plugin Maven que sería más apropiado para esta tarea?

Gracias!

Respuesta

4

sí, puede usar el complemento de ensamblaje. 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

Hubo un momento en que les pedimos a otros que usen Maven para obtener solo una definición y mucha "convención" como resultado. Con Ant (y otras herramientas de compilación también) sería una obviedad ... :-( – mliebelt

+1

bien, hay pocos beneficios para maven, ¿solo tienes que encontrarlos? La decisión de utilizar maven en nuestros proyectos no es el mío, así que tengo que amarlo y dar lo mejor de mí –

+1

¡Gracias por la respuesta detallada! Con unos pocos ajustes, sus muestras funcionan perfectamente para nuestro proyecto. Esto significa que podemos mover la construcción de Java al servidor de compilación. gran mejora en nuestro proceso y estabilidad de la aplicación. De nuevo, ¡muchas gracias! – HeavyE

1

No es exactamente lo que quiere, pero si se utiliza el objetivo war:war (con -DfailOnMissingWebXml=false) pondrá las dependencias en el directorio WEB-INF/lib en su carpeta target.

También puede consultar el dependency plugin.

+0

Como usted ha dicho, no es exactamente lo que estamos buscando, pero vamos a dejar a automatizar la construcción, por lo que 1! Si no escucho pronto una solución totalmente de Maven, definitivamente obtendrá crédito de respuesta. Gracias artbristol! – HeavyE

Cuestiones relacionadas