Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: usefinalname #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class CopyDependency extends ArtifactItem
// should be applied while traversing the dependencies tree
Boolean stripVersion // Whether version number should be removed from file names
Boolean stripTimestamp // Whether snapshot timestamp should be removed from file names
Boolean useFinalName // Whether the file name is set to finalName from the artifact's POM
Boolean excludeTransitive // Whether transitive dependencies should be excluded
int depth = -1 // Depth of transitive arguments

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.goldin.plugins.copy

import static com.github.goldin.plugins.common.ConversionUtils.*
import static com.github.goldin.plugins.common.GMojoUtils.*
import com.github.goldin.gcommons.beans.ExecOption
import com.github.goldin.gcommons.util.GroovyConfig
Expand All @@ -9,11 +10,14 @@ import com.github.goldin.plugins.common.Replace
import groovy.io.FileType
import org.apache.maven.plugin.MojoExecutionException
import org.apache.maven.plugins.annotations.*
import org.apache.maven.model.building.*
import org.apache.maven.model.resolution.ModelResolver;
import org.apache.maven.project.MavenProjectHelper
import org.apache.maven.shared.filtering.MavenFileFilter
import org.codehaus.plexus.util.FileUtils
import org.gcontracts.annotations.Ensures
import org.gcontracts.annotations.Requires
import org.apache.maven.artifact.Artifact


/**
Expand All @@ -34,6 +38,9 @@ class CopyMojo extends BaseGroovyMojo
@Component ( role = MavenFileFilter, hint = 'default' )
private MavenFileFilter fileFilter

@Component
private ModelBuilder modelBuilder

/**
* User-provided fields
*/
Expand Down Expand Up @@ -64,7 +71,6 @@ class CopyMojo extends BaseGroovyMojo

@Parameter ( required = false )
private boolean parallelDownload = false

@Parameter ( required = false )
private String customArchiveFormats

Expand Down Expand Up @@ -407,6 +413,21 @@ class CopyMojo extends BaseGroovyMojo
( d.destFileName && ( d.destFileName != f.name )) ? d.destFileName : /* the one from <dependency> but not default one, set by Maven */
( destFileName ) ? destFileName : /* the one from <resource> */
f.name

if ( d.useFinalName || useFinalName )
{
def resolver = new PomResolver(this, verbose, failIfNotFound)
DefaultModelBuildingRequest request = new DefaultModelBuildingRequest()
request.validationLevel = ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL
request.modelResolver = resolver
request.modelSource = resolver.resolveModel(d.groupId, d.artifactId, d.version)
def name = modelBuilder.build(request)?.effectiveModel?.build?.finalName
if (name)
{
destFileName = name + (d.classifier ? "-${d.classifier}" : '') + '.' + fileBean().extension( f )
}
}

if ( d.stripVersion || isStripVersion )
{
if ( d.version.endsWith( '-SNAPSHOT' ))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ final class CopyResource extends Resource implements Cloneable
Boolean parallelDownload
Boolean stripVersion
Boolean stripTimestamp
Boolean useFinalName
Boolean verbose
Boolean failIfNotFound
Boolean failOnError
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.github.goldin.plugins.copy

import com.github.goldin.plugins.common.BaseGroovyMojo
import com.github.goldin.plugins.common.ConversionUtils

import org.apache.maven.model.resolution.ModelResolver
import org.apache.maven.model.Repository
import org.apache.maven.model.building.FileModelSource
import org.apache.maven.model.building.ModelSource
import org.apache.maven.model.resolution.UnresolvableModelException
import org.apache.maven.artifact.InvalidRepositoryException

/**
* Resolves Maven POM files.
*/
class PomResolver implements ModelResolver {

BaseGroovyMojo baseMojo;
boolean verbose;
boolean failIfNotFound;

PomResolver(BaseGroovyMojo mojo, boolean verbose, boolean failIfNotFound) {
this.baseMojo = mojo
this.verbose = verbose
this.failIfNotFound = failIfNotFound
}

/**
* Tries to resolve the POM for the specified coordinates.
*
* @param groupId The group identifier of the POM, must not be {@code null}.
* @param artifactId The artifact identifier of the POM, must not be {@code null}.
* @param version The version of the POM, must not be {@code null}.
* @return The source of the requested POM, never {@code null}.
* @throws UnresolvableModelException If the POM could not be resolved from any configured repository.
*/
FileModelSource resolveModel( String groupId, String artifactId, String version ) throws UnresolvableModelException
{
def artifact = baseMojo.downloadArtifact( ConversionUtils.toMavenArtifact(groupId, artifactId, version, '', 'pom', '', false), verbose, failIfNotFound)
new FileModelSource(artifact.file)
}

/**
* Adds a repository to use for subsequent resolution requests. The order in which repositories are added matters,
* repositories that were added first should also be searched first. When multiple repositories with the same
* identifier are added, only the first repository being added will be used.
*
* @param repository The repository to add to the internal search chain, must not be {@code null}.
* @throws InvalidRepositoryException If the repository could not be added (e.g. due to invalid URL or layout).
*/
void addRepository( Repository repository ) throws InvalidRepositoryException
{
// This is ignored, we use the repos as grabbed from our Maven session in BaseGroovyMojo
}

/**
* Clones this resolver for usage in a forked resolution process. In general, implementors need not provide a deep
* clone. The only requirement is that invocations of {@link #addRepository(Repository)} on the clone do not affect
* the state of the original resolver and vice versa.
*
* @return The cloned resolver, never {@code null}.
*/
ModelResolver newCopy()
{
// We don't hold no unique state
return this;
}
}