2011-10-20 11 views
39

Załóżmy, że trzeba zarządzać artefakt, który składa się z aribtrary struktury folderów/plików rzutowane jako archiwum zip. Nie jest dla mnie jasne, jak to osiągnąć w Maven w sposób, który najlepiej pasuje do "sposobu Maven".Maven najlepsze praktyki tworzenia ad hoc zip artefakt

wiem, że nie ma „zip” rodzaj opakowania. Czy oznacza to, że nie ma ogólnego cyklu życia w Maven, aby po prostu wziąć to, co mam w folderze zasobów, zip go i zainstalować/wdrożyć go do moich repozytoriów?

Poszukuję opcji, z oceną, w jaki sposób każda opcja spełnia mój wymóg, by podążać drogą maven, ponieważ nie chcę ponosić oczywistych kar za odejście od złotej ścieżki. . .

+0

Można to zrobić bez "klasyfikatora" tutaj: https://stackoverflow.com/questions/25078028/how-to-create-zip-target-instead-of-jar-in-maven – Adam

Odpowiedz

52

Zdecyduj, jakiego klasyfikatora użyjesz do swojego pliku zip, dla dobra argumentu powiedzmy, że będzie to sample.

W projekcie utworzyć plik assembly/sample.xml

Wypełnij assembly/sample.xml coś takiego:

<?xml version="1.0" encoding="UTF-8"?> 
<assembly 
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
    http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 
     http://maven.apache.org/xsd/assembly-1.1.2.xsd" 
> 
    <id>sample</id> 
    <formats> 
    <format>zip</format> 
    </formats> 
    <fileSets> 
    <fileSet> 
     <outputDirectory>/</outputDirectory> 
     <directory>some/directory/in/your/project</directory> 
    </fileSet> 
    </fileSets> 
    <!-- use this section if you want to package dependencies --> 
    <dependencySets> 
    <dependencySet> 
     <outputDirectory>lib</outputDirectory> 
     <excludes> 
     <exclude>*:pom</exclude> 
     </excludes> 
     <useStrictFiltering>true</useStrictFiltering> 
     <useProjectArtifact>false</useProjectArtifact> 
     <scope>runtime</scope> 
    </dependencySet> 
    </dependencySets> 
</assembly> 

dodać go do POM za build sekcji

<build> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-assembly-plugin</artifactId> 
     <executions> 
      <execution> 
      <id>create-distribution</id> 
      <phase>package</phase> 
      <goals> 
       <goal>single</goal> 
      </goals> 
      <configuration> 
       <descriptors> 
       <descriptor>assembly/sample.xml</descriptor> 
       </descriptors> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 

W efekcie powinno to stworzyć i zainstaluj you-project-name-VERSION-sample.zip.

Proponuję przeczytać rozdział o zgromadzeniach z Sonatype za maven książki: https://books.sonatype.com/mvnref-book/reference/assemblies.html

Również przeczytać specyfikację formatu montaż: http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

+2

Ale co packagin typ g byłby najbardziej odpowiedni do użycia w pom? A może po prostu użyj wartości domyślnej (java), nawet jeśli nie ma źródła Java. – William

+10

@ William. Jeśli nie ma kodu Java powiązanego z projektem, użyj pom

0

Wypełnij montażu/sample.xml z mniej więcej tak:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> 
    <id>install</id> 
    <baseDirectory>/</baseDirectory> 
    <formats> 
    <format>zip</format> 
</formats> 
<fileSets> 
    <fileSet> 
    <includes> 
     <include>README*</include> 
     <include>*.sh</include> 
    </includes> 
    </fileSet> 
    <fileSet> 
    <directory>target/classes/</directory> 
    <outputDirectory>resources</outputDirectory> 
    <includes> 
     <include>*.properties</include> 
     <include>*.xml</include> 
    </includes> 
    <lineEnding>dos</lineEnding> 
    </fileSet> 
    <!-- 
    <fileSet> 
    <directory>target/</directory> 
    <outputDirectory>lib</outputDirectory> 
    <includes> 
     <include>*.jar</include> 
    </includes> 
    <lineEnding>dos</lineEnding> 
    </fileSet> 
    --> 
</fileSets> 
<!-- use this section if you want to package dependencies --> 
    <dependencySets> 
    <dependencySet> 
     <outputDirectory>lib</outputDirectory> 
     <excludes> 
     <exclude>*:pom</exclude> 
     </excludes> 
     <useStrictFiltering>true</useStrictFiltering> 
     <useProjectArtifact>true</useProjectArtifact> 
     <scope>runtime</scope> 
    </dependencySet> 
    </dependencySets> 
</assembly> 

dodać poniższe kody w pliku pom.xml. Polecenia

<plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <descriptor>assembly/sample.xml</descriptor> 
      </configuration> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>single</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 

prowadzony jako mvn package -P dev -Dmaven.test.skip

stworzy pliki zip, które zawierają: xxxx.zip -lib/.jar -ŹRÓDŁA/ .xml/Właściwości -readme -Start. sh

+0

Już odpowiedziałeś na pytanie. Nie masz żadnych dodatkowych informacji. – lpratlong