11

I've maven project. I use maven-assembly-plugin to create zip files with all module dependencies packed in it. Need to create zip with following structure:

/my-libs
/other-libs

To my-libs need to pack dependencies from my-lib depenency + all its transitive dependencies. TO other-libs need to pack all other dependencies from current maven module.

Basically I need conditionally select target folder:

if (dependency in transitive-dependencies(my-lib))
   copy to /my-libs
else
   copy to /other-libs

Is it possible to do with maven-assembly-plugin ? Are there any alternative maven plugins to do so?

turbanoff
  • 2,439
  • 6
  • 42
  • 99
  • Is 'mylib' the maven project itself or is it used as a dependency in your maven project? – thuri Feb 14 '19 at 07:24
  • So I assume `my-lib` expresses the projects artifacts within a multi module build and `other-libs` are the transitive dependencies of your project? The question is why do you need such a packaging setup? – khmarbaise Feb 14 '19 at 09:03
  • I need to build single artifact with 2 pack of libraries. This artifacts will be delivered to platform and unpacked. `my-libs` directory will be added to classpath to one set of applications, while `my-libs` + `other-libs` will be used to be added to classpath to other set of applications. – turbanoff Feb 14 '19 at 09:17
  • with assembly-plugin?: not in a dynamic way: you have to hardcode/wildcard everything you need in assembly.xml. alternative plugin?: nothing commonly known (to me), but i am sure it is possible with a "custom plugin". – xerx593 Feb 15 '19 at 00:55
  • ..but to achieve what you want, I would recommend to better utilize "parent pom" , "modules" and "pom import" ... – xerx593 Feb 15 '19 at 00:59
  • Not necessary assembly-plugin. Is there any other plugin which can split dependencies? Not sure how import pom can help there. Can you explain in more details? – turbanoff Feb 15 '19 at 07:53

3 Answers3

3

You need to define two dependencySet in the assembly XML file

<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">
    ...                                                                                                                        
    <dependencySets>                                                                                                                         
        <dependencySet>                                                                                                                      
            <useProjectArtifact>false</useProjectArtifact>                                                                                   
            <useTransitiveFiltering>false</useTransitiveFiltering>                                                                            
            <includes>                                                                                                                       
                <include>com.example:dependency1:jar</include>                                                                       
                <include>com.example:dependency2:jar</include>                                                                       
                ...
            </includes>                                                                                                                      
            <outputDirectory>/my-libs</outputDirectory>                                                                                             
        </dependencySet>                                                                                                                     
        <dependencySet>                                                                                                                      
            <useProjectArtifact>false</useProjectArtifact>                                                                                   
            <useTransitiveFiltering>false</useTransitiveFiltering>                                                                            
            <includes>                                                                                                                       
                <include>com.example:other1:jar</include>                                                                       
                <include>com.example:other2:jar</include>                                                                       
                ...
            </includes>                                                                                                                      
            <outputDirectory>/other-libs</outputDirectory>                                                                                             
        </dependencySet>                                                                                                                     
    </dependencySets>         
    ...                                                                                                               
</assembly>   

Docs:

http://maven.apache.org/plugins/maven-assembly-plugin/usage.html

http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

isalgueiro
  • 1,973
  • 16
  • 20
  • Is it possible to avoid manually list all dependencies? I know about that solution, but it's too _fragile_. Any new transitive dependency will break idea. – turbanoff Feb 15 '19 at 18:26
  • Oh, sorry, then you need to set useTransitiveFiltering to false. By default the filters don't apply to transitive deps and the plugin just pulls all of them. I'll edit my answer. – isalgueiro Feb 16 '19 at 08:59
  • @turbanoff Use one `dependencySet` with `includes/include`:`my-lib` and one `dependencySet` with `excludes/exclude`:`my-lib`. – Marinos An Mar 03 '20 at 12:04
0

I got the impression that you started the wrong way. You say that only some programs need other-libs on the classpath, others not. So these seem not to be "real" dependencies of your project, at most optional ones.

I would suggest to use the my-lib project to create an assembly zip of my-lib with all dependencies. This can be used in all contexts where otherlibs are not necessary.

On the other hand, you can create a second zip from your original project, containing all the libs. This way, you have two different zips for two different purposes, but you do not need to create a directory structure inside the zip.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • It leads to duplicate libraries in zips. It's what I'm actually trying to avoid. – turbanoff Feb 19 '19 at 13:09
  • @turbanoff Why? You would have a clear separation for different use cases, and that little bit of disk space should not do harm. – J Fabian Meier Feb 19 '19 at 13:12
  • @turbanoff I would still like to understand why you want to avoid duplicate libraries. Is this some kind of embedded system with little disk space? – J Fabian Meier Mar 06 '20 at 12:38
0

You can split your dependencies into two modules:

- ModuleX
  pom.xml (contain my-libs)
- ModuleY
  pom.xml (contain my-libs + other-libs)

Then use maven-dependency-plugin to copy transitives dependencies into respective folder.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.parent.basedir}/build/other-lib</outputDirectory>
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Now you each module build will contains exactly only the jars you need, no duplicate INSIDE. (I think we can ignore the duplication of my-libs BETWEENboth build because you don't gonna use both in the same platform anyway)

Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51