Maven unpack to disregard original folder

2019-08-03 06:43发布

One of my projects has a structure like this:

.\ca\<modulename>\dist\<modulename>.zip

I want to make a package that just has the zip files in a single package, so if I have submodules mod1, mod2, modn, I'll end up with:

ZIP CONTENT:
  mod1.zip
  mod2.zip
  ...
  modn.zip

What I've done so far is create a maven project and, using maven dependency and assembly plugins I got to this:

<plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>

                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>es.xunta.emprego.cntxes</groupId>
                                    <artifactId>myproject-cas</artifactId>
                                    <version>${cas.version}</version>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>target/dependency</outputDirectory>
                                    <type>zip</type>
                                    <includes>ca/**/dist/*.zip</includes>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptor>src/main/assembly/zip.xml</descriptor>
                    <appendAssemblyId>false</appendAssemblyId>
                </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>
                    </execution>
                </executions>
            </plugin>
        </plugins>

With this being assembly/zip.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<assembly>
    <id>bin</id>
    <formats>
        <!-- formato de salida del empaquetado -->
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <outputDirectory>/</outputDirectory>
            <directory>target/dependency</directory>
            <includes>
                <include>/**/dist/*.zip</include>
            </includes>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
    </fileSets>
</assembly>  

But this is respecting my original zip's folder structure, so the result is:

ZIP CONTENT
   ca\mod1\dist\mod1.zip
   ca\mod2\dist\mod2.zip
   ...
   ca\modn\dist\modn.zip

It might seem a small issue but the number of modules is big and gathering the different zip files is very annoying having to browse through every folder. I've been struggling with assembly and dependency but haven't found a way to achieve what I want. Note that I'm using wildcards (ca/**/dist/*.zip) because I don't know prior to compilation the name of the files that will be there.

Any help please?

标签: java maven
3条回答
The star\"
2楼-- · 2019-08-03 07:03

Are mod1.zip, mod2.zip, etc. installed, or can they be installed into your local repository? You could use the maven-dependency-plugin:

<dependencies>
    <dependency>
        <groupId>com.whatever</groupId>
        <artifactId>mod1</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <type>zip</type>
    <dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.basedir}/dist</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

This gets you back to maintaining this dependency list given a dynamic module list, but if you can figure out how to make the dependency list dynamic, then this might get you farther.

查看更多
疯言疯语
3楼-- · 2019-08-03 07:08

You need a plugin between the dependency:unpack and the assembly:single task that flattens your resources.

You could fiddle with the resources:copy-resources task, but that probably won't do the trick, as confirmed here: Maven : copy files without subdirectory structure

For such cases, the maven ant plugin was created, as describe in @Fabien's answer, to invoke all sorts of non-standard behaviours.

查看更多
太酷不给撩
4楼-- · 2019-08-03 07:24

I don't know if there's a way to do this but this question might help you find a way through : execute ant / groovy script inside maven

According to the linked answer, here is an ANT example (I can't try it right now) with the maven ANT plugin :

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>unpack</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>unpack</goal>
                </goals>
                <configuration>
                    <artifactItems>
                        <artifactItem>
                            <groupId>es.xunta.emprego.cntxes</groupId>
                            <artifactId>myproject-cas</artifactId>
                            <version>${cas.version}</version>
                            <overWrite>true</overWrite>
                            <outputDirectory>target/dependency</outputDirectory>
                            <type>zip</type>
                            <includes>ca/**/dist/*.zip</includes>
                        </artifactItem>
                    </artifactItems>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <executions>
            <execution>
                <id>move</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <target>
                        <copy todir="${project.basedir}/target/dependency" flatten="true">
                            <fileset dir="${project.basedir}/ca">
                                <include name="**/dist/*.zip"/>
                            </fileset>
                        </copy>
                    </target>
                </configuration>
            </execution>
        </executions>
    </plugin>
    ...
</plugins>

At least, the version 3.0.3 of Maven is required in order to insure plugins execution order on same phase.

查看更多
登录 后发表回答