Class MavenHelper


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

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

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String cleanToBeTokenizedString​(java.lang.String str)
      Clean the pattern string for future regexp usage.
      static org.apache.maven.artifact.Artifact createArtifact​(java.lang.String groupId, java.lang.String artifactId, java.lang.String version, java.lang.String type)
      Creates an artifact instance for the supplied coordinates.
      static org.apache.maven.artifact.Artifact createArtifact​(java.lang.String groupId, java.lang.String artifactId, java.lang.String version, java.lang.String type, java.lang.String classifier)
      Creates an artifact instance for the supplied coordinates.
      static org.apache.maven.artifact.Artifact createArtifact​(java.lang.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, java.lang.String type, java.lang.String classifier)
      Creates an artifact instance for the supplied model.
      static java.util.List<org.apache.maven.artifact.Artifact> createAttachedArtifacts​(java.lang.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 java.io.File createZip​(java.util.Properties props, org.apache.maven.plugin.logging.Log log, java.lang.String duplicate, java.util.List<org.apache.tools.ant.types.ZipFileSet> fsets, java.io.File target)
      Create a zip file.
      static org.apache.tools.ant.types.ZipFileSet createZipFileSet​(java.io.File dir, java.lang.String includePatterns, java.lang.String excludePatterns)
      Create an Ant ZipFileSet.
      static org.apache.tools.ant.types.ZipFileSet createZipFileSet​(java.io.File dir, java.util.List<java.lang.String> includes, java.util.List<java.lang.String> excludes)
      Create an Ant ZipFileSet.
      static java.util.Set<org.apache.maven.artifact.Artifact> excludeTransitive​(java.util.Set<org.apache.maven.artifact.Artifact> artifacts, java.util.Set<org.apache.maven.artifact.Artifact> dependencyArtifacts)
      Filters a set of artifacts.
      static java.util.Set<org.apache.maven.artifact.Artifact> filterArtifacts​(java.util.Set<org.apache.maven.artifact.Artifact> artifacts, java.util.Set<org.apache.maven.artifact.Artifact> dependencyArtifacts, boolean excludeTransitive, java.lang.String includeScope, java.lang.String excludeScope, java.lang.String excludeTypes, java.lang.String includeTypes)
      Filters a set of artifacts.
      static java.util.Set<org.apache.maven.artifact.Artifact> filterArtifacts​(java.util.Set<org.apache.maven.artifact.Artifact> artifacts, java.util.Set<org.apache.maven.artifact.Artifact> dependencyArtifacts, boolean excludeTransitive, java.lang.String includeScope, java.lang.String excludeScope, java.lang.String excludeTypes, java.lang.String includeTypes, java.lang.String includeClassifiers, java.lang.String excludeClassifiers, java.lang.String includeGroupIds, java.lang.String excludeGroupIds, java.lang.String includeArtifactIds, java.lang.String excludeArtifactIds)
      Filters a set of artifacts.
      private static org.apache.maven.artifact.Artifact getArtifactFile​(java.lang.String dir, java.lang.String finalName, org.apache.maven.model.Model model)
      Search for a file matching an artifact for the given finalName and model.
      static java.util.List<java.lang.String> getCommaSeparatedList​(java.lang.String list)
      Convert a comma separated string into a list.
      static java.util.List<java.io.File> getFiles​(java.lang.String dirPath, java.lang.String includes, java.lang.String excludes)
      Return the files contained in the directory, using inclusion and exclusion ant patterns.
      private static java.lang.String getFinalName​(org.apache.maven.model.Model model)
      Get the final name of a project artifact from a Model instance.
      static java.io.File getPomInTarget​(java.lang.String dir)
      Returns the pom installed in target or null if not found.
      private static java.lang.String listToString​(java.util.List<java.lang.String> list)
      Convert a to a comma separated String.
      private static java.lang.String logUnpack​(java.io.File file, java.io.File location, java.lang.String includes, java.lang.String excludes)
      Create the logging message for an unpack invocation.
      static java.lang.String modelAsString​(org.apache.maven.model.Model model)
      Read a model as a String.
      static org.apache.maven.model.Model readModel​(java.io.File pom)
      Reads a given model.
      static org.eclipse.aether.resolution.ArtifactResult resolveArtifact​(java.lang.String groupId, java.lang.String artifactId, java.lang.String classifier, java.lang.String type, java.lang.String version, org.eclipse.aether.RepositorySystem repoSystem, org.eclipse.aether.RepositorySystemSession repoSession, java.util.List<org.eclipse.aether.repository.RemoteRepository> remoteRepos)
      Resolve a remote artifact using aether.
      static void unpack​(java.io.File file, java.io.File location, java.lang.String includes, java.lang.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​(java.io.File outfile, java.lang.StringBuilder input)
      Write the given input to a file.
      static void writePom​(org.apache.maven.model.Model model, java.io.File buildDir)
      Write a model to buildDir/${project.build.finalName}.pom.
      static void writePom​(org.apache.maven.model.Model model, java.io.File buildDir, java.lang.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 Detail

      • MavenHelper

        private MavenHelper()
        Cannot be instantiated.
    • Method Detail

      • readModel

        public static org.apache.maven.model.Model readModel​(java.io.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 java.lang.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 java.util.List<org.apache.maven.artifact.Artifact> createAttachedArtifacts​(java.lang.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​(java.lang.String dir,
                                                                          java.lang.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​(java.lang.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 java.io.File getPomInTarget​(java.lang.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 java.util.List<java.io.File> getFiles​(java.lang.String dirPath,
                                                            java.lang.String includes,
                                                            java.lang.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​(java.lang.String groupId,
                                                                        java.lang.String artifactId,
                                                                        java.lang.String version,
                                                                        java.lang.String type,
                                                                        java.lang.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,
                                                                        java.lang.String type,
                                                                        java.lang.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​(java.lang.String groupId,
                                                                        java.lang.String artifactId,
                                                                        java.lang.String version,
                                                                        java.lang.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,
                                    java.io.File buildDir)
                             throws java.io.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:
        java.io.IOException - if an error occurred while writing the file
      • writePom

        public static void writePom​(org.apache.maven.model.Model model,
                                    java.io.File buildDir,
                                    java.lang.String pomFileName)
                             throws java.io.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:
        java.io.IOException - if an error occurred while writing the file
      • modelAsString

        public static java.lang.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 java.util.Set<org.apache.maven.artifact.Artifact> filterArtifacts​(java.util.Set<org.apache.maven.artifact.Artifact> artifacts,
                                                                                        java.util.Set<org.apache.maven.artifact.Artifact> dependencyArtifacts,
                                                                                        boolean excludeTransitive,
                                                                                        java.lang.String includeScope,
                                                                                        java.lang.String excludeScope,
                                                                                        java.lang.String excludeTypes,
                                                                                        java.lang.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 java.util.Set<org.apache.maven.artifact.Artifact> excludeTransitive​(java.util.Set<org.apache.maven.artifact.Artifact> artifacts,
                                                                                          java.util.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 java.util.Set<org.apache.maven.artifact.Artifact> filterArtifacts​(java.util.Set<org.apache.maven.artifact.Artifact> artifacts,
                                                                                        java.util.Set<org.apache.maven.artifact.Artifact> dependencyArtifacts,
                                                                                        boolean excludeTransitive,
                                                                                        java.lang.String includeScope,
                                                                                        java.lang.String excludeScope,
                                                                                        java.lang.String excludeTypes,
                                                                                        java.lang.String includeTypes,
                                                                                        java.lang.String includeClassifiers,
                                                                                        java.lang.String excludeClassifiers,
                                                                                        java.lang.String includeGroupIds,
                                                                                        java.lang.String excludeGroupIds,
                                                                                        java.lang.String includeArtifactIds,
                                                                                        java.lang.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​(java.io.File file,
                                  java.io.File location,
                                  java.lang.String includes,
                                  java.lang.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 java.lang.String logUnpack​(java.io.File file,
                                                  java.io.File location,
                                                  java.lang.String includes,
                                                  java.lang.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​(java.lang.String groupId,
                                                                                   java.lang.String artifactId,
                                                                                   java.lang.String classifier,
                                                                                   java.lang.String type,
                                                                                   java.lang.String version,
                                                                                   org.eclipse.aether.RepositorySystem repoSystem,
                                                                                   org.eclipse.aether.RepositorySystemSession repoSession,
                                                                                   java.util.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 java.lang.String cleanToBeTokenizedString​(java.lang.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​(java.io.File outfile,
                                     java.lang.StringBuilder input)
                              throws java.io.IOException
        Write the given input to a file.
        Parameters:
        outfile - the file to write to
        input - the input to write
        Throws:
        java.io.IOException - if an error occurs while writing to the file
      • getCommaSeparatedList

        public static java.util.List<java.lang.String> getCommaSeparatedList​(java.lang.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 java.lang.String listToString​(java.util.List<java.lang.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​(java.io.File dir,
                                                                             java.util.List<java.lang.String> includes,
                                                                             java.util.List<java.lang.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​(java.io.File dir,
                                                                             java.lang.String includePatterns,
                                                                             java.lang.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 java.io.File createZip​(java.util.Properties props,
                                             org.apache.maven.plugin.logging.Log log,
                                             java.lang.String duplicate,
                                             java.util.List<org.apache.tools.ant.types.ZipFileSet> fsets,
                                             java.io.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