Package org.glassfish.build.utils
Class MavenHelper
- java.lang.Object
-
- org.glassfish.build.utils.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 fortarget/${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 fortarget/${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 AntZipFileSet
.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 AntZipFileSet
.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 givenfinalName
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 aModel
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 separatedString
.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 aString
.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 tobuildDir/${project.build.finalName}.pom
.static void
writePom(org.apache.maven.model.Model model, java.io.File buildDir, java.lang.String pomFileName)
Write the model tobuildDir/${project.build.finalName}.pom
.
-
-
-
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 anIOException
occurs
-
getFinalName
private static java.lang.String getFinalName(org.apache.maven.model.Model model)
Get the final name of a project artifact from aModel
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 fortarget/${project.build.finalName}-*.*
.- Parameters:
dir
- ${project.build.directory}artifact
- the main artifact for which corresponding attached artifacts will be searchedmodel
- 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 givenfinalName
and model.- Parameters:
dir
- the directory to searchfinalName
- the artifact final namemodel
- 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 fortarget/${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 scanincludes
- the includes pattern, comma separatedexcludes
- 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 groupIdartifactId
- The artifactIdversion
- The versiontype
- 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 modeltype
- the type of the artifactclassifier
- 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 groupIdartifactId
- The artifactIdversion
- The versiontype
- 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 tobuildDir/${project.build.finalName}.pom
.- Parameters:
model
- an instance of modelbuildDir
- 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 tobuildDir/${project.build.finalName}.pom
.- Parameters:
model
- an instance of modelbuildDir
- the directory in which to write the pompomFileName
- 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 aString
.- 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 filterdependencyArtifacts
- the set of artifact representing direct dependenciesexcludeTransitive
- exclude transitive dependenciesincludeScope
- the scopes to include, comma separated, can be nullexcludeScope
- the scopes to exclude, comma separated, can be nullexcludeTypes
- the types to include, comma separated, can be nullincludeTypes
- 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 filterdependencyArtifacts
- 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 filterdependencyArtifacts
- the set of artifact representing direct dependenciesexcludeTransitive
- exclude transitive dependenciesincludeScope
- the scopes to include, comma separated, can benull
excludeScope
- the scopes to exclude, comma separated, can benull
excludeTypes
- the types to exclude, comma separated, can benull
includeTypes
- the types to include, comma separated, can benull
includeClassifiers
- the classifiers to include, comma separated, can benull
excludeClassifiers
- the classifiers to exclude, comma separated, can benull
includeGroupIds
- the groupIds to include, comma separated, can benull
excludeGroupIds
- the groupIds to exclude, comma separated, can benull
includeArtifactIds
- the artifactIds to include, comma separated, can benull
excludeArtifactIds
- the artifactIds to exclude, comma separated, can benull
- 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 unpacklocation
- the directory where to unpackincludes
- includes pattern for the files to unpackexcludes
- exclude pattern for the files to unpacksilent
- log unpack or notlog
- the Maven logger instance, can be nullarchiverManager
- 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 unpackedlocation
- the target directory for the unpackincludes
- the include patternsexcludes
- 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 benull
artifactId
- the artifact identifier of the artifact, may benull
classifier
- the classifier of the artifact, may benull
type
- the type of the artifact, may benull
version
- the version of the artifact, may benull
repoSystem
- the repository system componentrepoSession
- the repository session componentremoteRepos
- 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 toinput
- 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 separatedString
.- Parameters:
list
- theList
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 AntZipFileSet
.- Parameters:
dir
- the resource directoryincludes
- the list of include patternsexcludes
- 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 AntZipFileSet
.- Parameters:
dir
- the resource directoryincludePatterns
- the include patterns in comma separated listexcludePatterns
- 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 propertieslog
- Maven loggerduplicate
- behavior for duplicate file, one of "add", "preserve" or "fail"fsets
- list ofZipFileSet
that describe the resources to ziptarget
- theFile
instance for the zip file to create- Returns:
- the target file
-
-