Class MavenHelper

java.lang.Object
org.glassfish.build.utils.MavenHelper

public final class MavenHelper extends Object
Helper for common Maven Plugin tasks.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Cannot be instantiated.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Clean the pattern string for future regexp usage.
    static org.apache.maven.artifact.Artifact
    createArtifact(String groupId, String artifactId, String version, String type)
    Creates an artifact instance for the supplied coordinates.
    static org.apache.maven.artifact.Artifact
    createArtifact(String groupId, String artifactId, String version, String type, String classifier)
    Creates an artifact instance for the supplied coordinates.
    static org.apache.maven.artifact.Artifact
    createArtifact(String dir, org.apache.maven.model.Model model)
    Create an artifact and its associated file by searching for target/${project.build.finalName}.${project.packaging}.
    static org.apache.maven.artifact.Artifact
    createArtifact(org.apache.maven.model.Dependency dep)
    Creates an artifact instance from a dependency object.
    static org.apache.maven.artifact.Artifact
    createArtifact(org.apache.maven.model.Model model)
    Creates an artifact instance for the supplied model.
    static org.apache.maven.artifact.Artifact
    createArtifact(org.apache.maven.model.Model model, String type, String classifier)
    Creates an artifact instance for the supplied model.
    static List<org.apache.maven.artifact.Artifact>
    createAttachedArtifacts(String dir, org.apache.maven.artifact.Artifact artifact, org.apache.maven.model.Model model)
    Create a list of attached artifacts and their associated files by searching for target/${project.build.finalName}-*.*.
    static File
    createZip(Properties props, org.apache.maven.plugin.logging.Log log, String duplicate, List<org.apache.tools.ant.types.ZipFileSet> fsets, File target)
    Create a zip file.
    static org.apache.tools.ant.types.ZipFileSet
    createZipFileSet(File dir, String includePatterns, String excludePatterns)
    Create an Ant ZipFileSet.
    static org.apache.tools.ant.types.ZipFileSet
    createZipFileSet(File dir, List<String> includes, List<String> excludes)
    Create an Ant ZipFileSet.
    static Set<org.apache.maven.artifact.Artifact>
    excludeTransitive(Set<org.apache.maven.artifact.Artifact> artifacts, Set<org.apache.maven.artifact.Artifact> dependencyArtifacts)
    Filters a set of artifacts.
    static Set<org.apache.maven.artifact.Artifact>
    filterArtifacts(Set<org.apache.maven.artifact.Artifact> artifacts, Set<org.apache.maven.artifact.Artifact> dependencyArtifacts, boolean excludeTransitive, String includeScope, String excludeScope, String excludeTypes, String includeTypes)
    Filters a set of artifacts.
    static Set<org.apache.maven.artifact.Artifact>
    filterArtifacts(Set<org.apache.maven.artifact.Artifact> artifacts, Set<org.apache.maven.artifact.Artifact> dependencyArtifacts, boolean excludeTransitive, String includeScope, String excludeScope, String excludeTypes, String includeTypes, String includeClassifiers, String excludeClassifiers, String includeGroupIds, String excludeGroupIds, String includeArtifactIds, String excludeArtifactIds)
    Filters a set of artifacts.
    private static org.apache.maven.artifact.Artifact
    getArtifactFile(String dir, String finalName, org.apache.maven.model.Model model)
    Search for a file matching an artifact for the given finalName and model.
    static List<String>
    Convert a comma separated string into a list.
    static List<File>
    getFiles(String dirPath, String includes, String excludes)
    Return the files contained in the directory, using inclusion and exclusion ant patterns.
    private static String
    getFinalName(org.apache.maven.model.Model model)
    Get the final name of a project artifact from a Model instance.
    static File
    Returns the pom installed in target or null if not found.
    private static String
    Convert a to a comma separated String.
    private static String
    logUnpack(File file, File location, String includes, String excludes)
    Create the logging message for an unpack invocation.
    static String
    modelAsString(org.apache.maven.model.Model model)
    Read a model as a String.
    static org.apache.maven.model.Model
    Reads a given model.
    static org.eclipse.aether.resolution.ArtifactResult
    resolveArtifact(String groupId, String artifactId, String classifier, String type, String version, org.eclipse.aether.RepositorySystem repoSystem, org.eclipse.aether.RepositorySystemSession repoSession, List<org.eclipse.aether.repository.RemoteRepository> remoteRepos)
    Resolve a remote artifact using aether.
    static void
    unpack(File file, File location, String includes, String excludes, boolean silent, org.apache.maven.plugin.logging.Log log, org.codehaus.plexus.archiver.manager.ArchiverManager archiverManager)
    Unpacks a given file.
    static void
    writeFile(File outfile, StringBuilder input)
    Write the given input to a file.
    static void
    writePom(org.apache.maven.model.Model model, File buildDir)
    Write a model to buildDir/${project.build.finalName}.pom.
    static void
    writePom(org.apache.maven.model.Model model, File buildDir, String pomFileName)
    Write the model to buildDir/${project.build.finalName}.pom.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MavenHelper

      private MavenHelper()
      Cannot be instantiated.
  • Method Details

    • readModel

      public static org.apache.maven.model.Model readModel(File pom) throws org.apache.maven.plugin.MojoExecutionException
      Reads a given model.
      Parameters:
      pom - the pom File
      Returns:
      an instance of Model
      Throws:
      org.apache.maven.plugin.MojoExecutionException - if an IOException occurs
    • getFinalName

      private static String getFinalName(org.apache.maven.model.Model model)
      Get the final name of a project artifact from a Model instance.
      Parameters:
      model - the model
      Returns:
      the final name of the artifact
    • createAttachedArtifacts

      public static List<org.apache.maven.artifact.Artifact> createAttachedArtifacts(String dir, org.apache.maven.artifact.Artifact artifact, org.apache.maven.model.Model model) throws org.apache.maven.plugin.MojoExecutionException
      Create a list of attached artifacts and their associated files by searching for target/${project.build.finalName}-*.*.
      Parameters:
      dir - ${project.build.directory}
      artifact - the main artifact for which corresponding attached artifacts will be searched
      model - an instance of model
      Returns:
      the list of attached artifacts
      Throws:
      org.apache.maven.plugin.MojoExecutionException - if an error occurred while searching files
    • getArtifactFile

      private static org.apache.maven.artifact.Artifact getArtifactFile(String dir, String finalName, org.apache.maven.model.Model model) throws org.apache.maven.plugin.MojoExecutionException
      Search for a file matching an artifact for the given finalName and model.
      Parameters:
      dir - the directory to search
      finalName - the artifact final name
      model - the project model
      Returns:
      the Artifact instance
      Throws:
      org.apache.maven.plugin.MojoExecutionException - if an error occurred while searching files
    • createArtifact

      public static org.apache.maven.artifact.Artifact createArtifact(String dir, org.apache.maven.model.Model model) throws org.apache.maven.plugin.MojoExecutionException
      Create an artifact and its associated file by searching for target/${project.build.finalName}.${project.packaging}.
      Parameters:
      dir - ${project.build.directory}
      model - an instance of model
      Returns:
      the created Artifact instance
      Throws:
      org.apache.maven.plugin.MojoExecutionException - if an error occurred while searching files
    • getPomInTarget

      public static File getPomInTarget(String dir) throws org.apache.maven.plugin.MojoExecutionException
      Returns the pom installed in target or null if not found.
      Parameters:
      dir - ${project.build.directory}
      Returns:
      an instance of the pom file or null if not found
      Throws:
      org.apache.maven.plugin.MojoExecutionException - if an error occurred while searching files
    • getFiles

      public static List<File> getFiles(String dirPath, String includes, String excludes) throws org.apache.maven.plugin.MojoExecutionException
      Return the files contained in the directory, using inclusion and exclusion ant patterns.
      Parameters:
      dirPath - the directory to scan
      includes - the includes pattern, comma separated
      excludes - the excludes pattern, comma separated
      Returns:
      the list of files found
      Throws:
      org.apache.maven.plugin.MojoExecutionException - if an IOException occurred
    • createArtifact

      public static org.apache.maven.artifact.Artifact createArtifact(String groupId, String artifactId, String version, String type, String classifier)
      Creates an artifact instance for the supplied coordinates.
      Parameters:
      groupId - The groupId
      artifactId - The artifactId
      version - The version
      type - The type of the artifact. e.g "jar", "war" or "zip"
      classifier - The classifier
      Returns:
      the created artifact
    • createArtifact

      public static org.apache.maven.artifact.Artifact createArtifact(org.apache.maven.model.Model model, String type, String classifier)
      Creates an artifact instance for the supplied model.
      Parameters:
      model - the model
      type - the type of the artifact
      classifier - the classifier to use
      Returns:
      the created artifact
    • createArtifact

      public static org.apache.maven.artifact.Artifact createArtifact(org.apache.maven.model.Model model)
      Creates an artifact instance for the supplied model.
      Parameters:
      model - the model
      Returns:
      the created artifact
    • createArtifact

      public static org.apache.maven.artifact.Artifact createArtifact(String groupId, String artifactId, String version, String type)
      Creates an artifact instance for the supplied coordinates.
      Parameters:
      groupId - The groupId
      artifactId - The artifactId
      version - The version
      type - The type of the artifact. e.g "jar", "war" or "zip"
      Returns:
      the created artifact
    • createArtifact

      public static org.apache.maven.artifact.Artifact createArtifact(org.apache.maven.model.Dependency dep)
      Creates an artifact instance from a dependency object.
      Parameters:
      dep - the dependency object
      Returns:
      the created artifact
    • writePom

      public static void writePom(org.apache.maven.model.Model model, File buildDir) throws IOException
      Write a model to buildDir/${project.build.finalName}.pom.
      Parameters:
      model - an instance of model
      buildDir - the directory in which to write the pom
      Throws:
      IOException - if an error occurred while writing the file
    • writePom

      public static void writePom(org.apache.maven.model.Model model, File buildDir, String pomFileName) throws IOException
      Write the model to buildDir/${project.build.finalName}.pom.
      Parameters:
      model - an instance of model
      buildDir - the directory in which to write the pom
      pomFileName - the name of the written pom
      Throws:
      IOException - if an error occurred while writing the file
    • modelAsString

      public static String modelAsString(org.apache.maven.model.Model model) throws org.apache.maven.plugin.MojoExecutionException
      Read a model as a String.
      Parameters:
      model - the model
      Returns:
      the model as a String
      Throws:
      org.apache.maven.plugin.MojoExecutionException - if an IOException occurred
    • filterArtifacts

      public static Set<org.apache.maven.artifact.Artifact> filterArtifacts(Set<org.apache.maven.artifact.Artifact> artifacts, Set<org.apache.maven.artifact.Artifact> dependencyArtifacts, boolean excludeTransitive, String includeScope, String excludeScope, String excludeTypes, String includeTypes) throws org.apache.maven.plugin.MojoExecutionException
      Filters a set of artifacts.
      Parameters:
      artifacts - the set of artifacts to filter
      dependencyArtifacts - the set of artifact representing direct dependencies
      excludeTransitive - exclude transitive dependencies
      includeScope - the scopes to include, comma separated, can be null
      excludeScope - the scopes to exclude, comma separated, can be null
      excludeTypes - the types to include, comma separated, can be null
      includeTypes - the types to exclude, comma separated, can be null
      Returns:
      the set of filtered artifacts
      Throws:
      org.apache.maven.plugin.MojoExecutionException - if an error occurred while filtering
    • excludeTransitive

      public static Set<org.apache.maven.artifact.Artifact> excludeTransitive(Set<org.apache.maven.artifact.Artifact> artifacts, Set<org.apache.maven.artifact.Artifact> dependencyArtifacts) throws org.apache.maven.plugin.MojoExecutionException
      Filters a set of artifacts.
      Parameters:
      artifacts - the set of artifacts to filter
      dependencyArtifacts - the set of artifact representing direct dependencies
      Returns:
      the set of filtered artifacts
      Throws:
      org.apache.maven.plugin.MojoExecutionException - if an error occurred while filtering
    • filterArtifacts

      public static Set<org.apache.maven.artifact.Artifact> filterArtifacts(Set<org.apache.maven.artifact.Artifact> artifacts, Set<org.apache.maven.artifact.Artifact> dependencyArtifacts, boolean excludeTransitive, String includeScope, String excludeScope, String excludeTypes, String includeTypes, String includeClassifiers, String excludeClassifiers, String includeGroupIds, String excludeGroupIds, String includeArtifactIds, String excludeArtifactIds) throws org.apache.maven.plugin.MojoExecutionException
      Filters a set of artifacts.
      Parameters:
      artifacts - the set of artifacts to filter
      dependencyArtifacts - the set of artifact representing direct dependencies
      excludeTransitive - exclude transitive dependencies
      includeScope - the scopes to include, comma separated, can be null
      excludeScope - the scopes to exclude, comma separated, can be null
      excludeTypes - the types to exclude, comma separated, can be null
      includeTypes - the types to include, comma separated, can be null
      includeClassifiers - the classifiers to include, comma separated, can be null
      excludeClassifiers - the classifiers to exclude, comma separated, can be null
      includeGroupIds - the groupIds to include, comma separated, can be null
      excludeGroupIds - the groupIds to exclude, comma separated, can be null
      includeArtifactIds - the artifactIds to include, comma separated, can be null
      excludeArtifactIds - the artifactIds to exclude, comma separated, can be null
      Returns:
      the set of filtered artifacts
      Throws:
      org.apache.maven.plugin.MojoExecutionException - if an error occurred while filtering
    • unpack

      public static void unpack(File file, File location, String includes, String excludes, boolean silent, org.apache.maven.plugin.logging.Log log, org.codehaus.plexus.archiver.manager.ArchiverManager archiverManager) throws org.apache.maven.plugin.MojoExecutionException
      Unpacks a given file.
      Parameters:
      file - the file to unpack
      location - the directory where to unpack
      includes - includes pattern for the files to unpack
      excludes - exclude pattern for the files to unpack
      silent - log unpack or not
      log - the Maven logger instance, can be null
      archiverManager - an instance of ArchiveManager
      Throws:
      org.apache.maven.plugin.MojoExecutionException - if an error occurred while unpacking
    • logUnpack

      private static String logUnpack(File file, File location, String includes, String excludes)
      Create the logging message for an unpack invocation.
      Parameters:
      file - the file being unpacked
      location - the target directory for the unpack
      includes - the include patterns
      excludes - the exclude patterns
      Returns:
      the created String
    • resolveArtifact

      public static org.eclipse.aether.resolution.ArtifactResult resolveArtifact(String groupId, String artifactId, String classifier, String type, String version, org.eclipse.aether.RepositorySystem repoSystem, org.eclipse.aether.RepositorySystemSession repoSession, List<org.eclipse.aether.repository.RemoteRepository> remoteRepos) throws org.apache.maven.plugin.MojoExecutionException
      Resolve a remote artifact using aether.
      Parameters:
      groupId - the group identifier of the artifact, may be null
      artifactId - the artifact identifier of the artifact, may be null
      classifier - the classifier of the artifact, may be null
      type - the type of the artifact, may be null
      version - the version of the artifact, may be null
      repoSystem - the repository system component
      repoSession - the repository session component
      remoteRepos - the remote repositories to use
      Returns:
      the resolved artifact
      Throws:
      org.apache.maven.plugin.MojoExecutionException - if an error occurred while resolving the artifact
    • cleanToBeTokenizedString

      public static String cleanToBeTokenizedString(String str)
      Clean the pattern string for future regexp usage.
      Parameters:
      str - the string to cleanup
      Returns:
      the cleaned string
    • writeFile

      public static void writeFile(File outfile, StringBuilder input) throws IOException
      Write the given input to a file.
      Parameters:
      outfile - the file to write to
      input - the input to write
      Throws:
      IOException - if an error occurs while writing to the file
    • getCommaSeparatedList

      public static List<String> getCommaSeparatedList(String list)
      Convert a comma separated string into a list.
      Parameters:
      list - the string containing items separated by comma(s)
      Returns:
      list
    • listToString

      private static String listToString(List<String> list)
      Convert a to a comma separated String.
      Parameters:
      list - the List to convert
      Returns:
      the resulting String
    • createZipFileSet

      public static org.apache.tools.ant.types.ZipFileSet createZipFileSet(File dir, List<String> includes, List<String> excludes)
      Create an Ant ZipFileSet.
      Parameters:
      dir - the resource directory
      includes - the list of include patterns
      excludes - the list of exclude patterns
      Returns:
      the create ZipFileSet
    • createZipFileSet

      public static org.apache.tools.ant.types.ZipFileSet createZipFileSet(File dir, String includePatterns, String excludePatterns)
      Create an Ant ZipFileSet.
      Parameters:
      dir - the resource directory
      includePatterns - the include patterns in comma separated list
      excludePatterns - the exclude patterns in comma separate list
      Returns:
      the create ZipFileSet
    • createZip

      public static File createZip(Properties props, org.apache.maven.plugin.logging.Log log, String duplicate, List<org.apache.tools.ant.types.ZipFileSet> fsets, File target)
      Create a zip file.
      Parameters:
      props - Ant project properties
      log - Maven logger
      duplicate - behavior for duplicate file, one of "add", "preserve" or "fail"
      fsets - list of ZipFileSet that describe the resources to zip
      target - the File instance for the zip file to create
      Returns:
      the target file