Uploaded image for project: 'Maven Dependency Plugin'
  1. Maven Dependency Plugin
  2. MDEP-13

[dependency-maven-plugin] only copy "direct" dependencies not transitive dependencies

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.0-alpha-1
    • None
    • None

    Description

      I would like to be able to copy only the direct dependies without the transitive dependencies for the following use cases:

      Using Merge Modules in InstallShield:

      a merge module bundles a jar into some re-usable object for InstallShield. So in the pom.xml of my merge module, I have a direct dependency on that perticular jar file. But that jar file also has it's own dependencies, I don't want to copy those other dependencies, I only want that jar file.

      Taking this further to building the final installer: this bundles all merge modules. So in the pom.xml of the installer, I have dependencies on merge modules. I don't want to copy any jar dependencies that are below that also.

      I already created a Mojo that does all I want but it would be better if this also included in the dependency plugin.

      This is the code I have:

      package com.mycomp.maven.plugin.msm;

      import org.apache.maven.artifact.Artifact ;
      import org.apache.maven.model.Dependency;
      import org.apache.maven.plugin.AbstractMojo;
      import org.apache.maven.plugin.MojoExecutionException;
      import org.apache.maven.plugin.MojoFailureException;
      import org.codehaus.plexus.util.FileUtils ;

      import java.io.File;
      import java.io.IOException;
      import java.util.ArrayList;
      import java.util.Collection;
      import java.util.Iterator;
      import java.util.List;

      /**

      • @goal process-resources
      • @requiresDependencyResolution
      • @description Copy the dependencies for the InstallShield Merge Module
        */
        public class MsmProcessResourcesMojo extends AbstractMojo
        {
        /**
      • @parameter expression="${ project.artifacts}"
      • @required
      • @read-only
        */
        private Collection artifacts;

      /**

      • @parameter expression="${project.dependencies}
      • @required
      • @read-only
        */
        private Collection dependencies;

      /**

      • @parameter expression="${project.build.directory}/resources"
      • @required
        */
        private File targetDirectory;

      /**

      • @parameter expression="${stripVersions}" default-value="true"
        */
        private boolean stripVersions;

      /**

      • @parameter expression="${limitToExtension}"
        */
        private String limitToExtension;

      /**

      • @parameter expression="${useSubDirectoryPerType}" default-value="true"
        */
        private boolean useSubDirectoryPerType;

      public MsmProcessResourcesMojo()
      {
      }

      public void execute() throws MojoExecutionException, MojoFailureException
      {
      try
      {
      getLog().info( "Process Resources for InstallShield Merge Module..." );
      List firstListArtifacts = getFirstLineArtifacts();
      Iterator iterator = firstListArtifacts.iterator();
      while (iterator.hasNext())
      {
      Artifact artifact = (Artifact)iterator.next();
      if (limitToExtension == null || limitToExtension.equals( artifact.getType() ))
      {
      File targetDir;
      if( useSubDirectoryPerType )

      { targetDir = new File( targetDirectory, artifact.getType() + "s" ); }

      else

      { targetDir = targetDirectory; }

      getLog().info( "Copying artifact: " + artifact.toString() );
      if (stripVersions)

      { FileUtils.copyFile( artifact.getFile(), new File( targetDir, artifact.getGroupId() + "." + artifact.getType() ) ); }

      else

      { FileUtils.copyFileToDirectory( artifact.getFile(), targetDir ); }

      }
      }
      }
      catch (IOException e)

      { throw new MojoExecutionException( "Error copying artifacts", e ); }

      }

      private List getFirstLineArtifacts()
      {
      List result = new ArrayList( dependencies.size() );
      Iterator iterator = artifacts.iterator();
      while (iterator.hasNext ())
      {
      Artifact artifact = (Artifact)iterator.next();
      if (artifactIsADirectDependency( artifact ))

      { result.add( artifact ); }

      }

      return result;
      }

      private boolean artifactIsADirectDependency( Artifact artifact )
      {
      boolean result = false;
      Iterator iterator = dependencies.iterator();
      while ( iterator.hasNext())
      {
      Dependency dependency = (Dependency)iterator.next();
      if (dependency.getGroupId().equals( artifact.getGroupId() )
      && dependency.getArtifactId ().equals( artifact.getArtifactId() ))

      { result = true; break; }

      }

      return result;
      }
      }

      Note that I have 2 additional options:

      • limitToExtension: if you only want to copy dependencies with a certain extension (e.g. only .msm or .dll files for example)
      • useSubDirectoryPerType: this will create a structure as follows (simular to m1 repo) :
      • root dir
      • dlls
      • mydll.dll
      • msms
      • mymsm.dll

      regards,

      Wim

      Attachments

        Activity

          People

            brianf Brian E Fox
            wim.deblauwe@gmail.com Wim Deblauwe
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: