Search in sources :

Example 11 with EnforcerRuleException

use of org.apache.maven.enforcer.rule.api.EnforcerRuleException in project admin-console-beta by connexta.

the class ArtifactSizeEnforcerRule method getArtifactPath.

private String getArtifactPath(EnforcerRuleHelper helper) throws EnforcerRuleException {
    String convertedArtifactLocation = artifactLocation;
    if (StringUtils.isNotEmpty(convertedArtifactLocation)) {
        helper.getLog().info(String.format("Using specified artifactLocation %s", convertedArtifactLocation));
    } else {
        try {
            helper.getLog().info("artifactLocation property not specified. Looking up artifact using maven properties.");
            String artifactId = (String) helper.evaluate(PROJECT_ARTIFACT_ID_PROP);
            String version = (String) helper.evaluate(PROJECT_VERSION_PROP);
            String packaging = getPackaging(helper);
            String buildDir = (String) helper.evaluate(PROJECT_BUILD_DIR_PROP);
            helper.getLog().debug(String.format(DEFAULT_ARTIFACT_INFO_MSG, artifactId, version, packaging, buildDir));
            convertedArtifactLocation = buildDir + File.separator + artifactId + "-" + version + "." + packaging;
            helper.getLog().debug(String.format("Complete generated artifact path: %s", convertedArtifactLocation));
        } catch (ExpressionEvaluationException e) {
            throw new EnforcerRuleException(e.getMessage());
        }
    }
    return convertedArtifactLocation;
}
Also used : ExpressionEvaluationException(org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException)

Example 12 with EnforcerRuleException

use of org.apache.maven.enforcer.rule.api.EnforcerRuleException in project semantic-versioning by jeluard.

the class AbstractEnforcerRule method execute.

@Override
public void execute(final EnforcerRuleHelper helper) throws EnforcerRuleException {
    final MavenProject project = getMavenProject(helper);
    if (shouldSkipRuleExecution(project)) {
        helper.getLog().debug("Skipping non " + AbstractEnforcerRule.JAR_ARTIFACT_TYPE + " or " + BUNDLE_ARTIFACT_TYPE + " artifact.");
        return;
    }
    final Artifact previousArtifact;
    final Artifact currentArtifact = validateArtifact(project.getArtifact());
    final Version current = Version.parse(currentArtifact.getVersion());
    try {
        final ArtifactRepository localRepository = (ArtifactRepository) helper.evaluate("${localRepository}");
        final String version;
        if (this.previousVersion != null) {
            version = this.previousVersion;
            helper.getLog().info("Version specified as <" + version + ">");
        } else {
            final ArtifactMetadataSource artifactMetadataSource = (ArtifactMetadataSource) helper.getComponent(ArtifactMetadataSource.class);
            final List<ArtifactVersion> availableVersions = getAvailableReleasedVersions(artifactMetadataSource, project, localRepository);
            final List<ArtifactVersion> availablePreviousVersions = filterNonPreviousVersions(availableVersions, current);
            if (availablePreviousVersions.isEmpty()) {
                helper.getLog().warn("No previously released version. Backward compatibility check not performed.");
                return;
            }
            version = availablePreviousVersions.iterator().next().toString();
            helper.getLog().info("Version deduced as <" + version + "> (among all availables: " + availablePreviousVersions + ")");
        }
        final ArtifactFactory artifactFactory = (ArtifactFactory) helper.getComponent(ArtifactFactory.class);
        previousArtifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), version, null, project.getArtifact().getType());
        final ArtifactResolver resolver = (ArtifactResolver) helper.getComponent(ArtifactResolver.class);
        resolver.resolve(previousArtifact, project.getRemoteArtifactRepositories(), localRepository);
        validateArtifact(previousArtifact);
    } catch (Exception e) {
        helper.getLog().warn("Exception while accessing artifacts; skipping check.", e);
        return;
    }
    final Version previous = Version.parse(previousArtifact.getVersion());
    final File previousJar = previousArtifact.getFile();
    final File currentJar = currentArtifact.getFile();
    compareJars(helper, previous, previousJar, current, currentJar);
}
Also used : DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) ArtifactFactory(org.apache.maven.artifact.factory.ArtifactFactory) MavenProject(org.apache.maven.project.MavenProject) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) Version(org.semver.Version) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) ArtifactMetadataSource(org.apache.maven.artifact.metadata.ArtifactMetadataSource) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact) ArtifactResolver(org.apache.maven.artifact.resolver.ArtifactResolver) ExpressionEvaluationException(org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException) IOException(java.io.IOException) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) ArtifactMetadataRetrievalException(org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException)

Example 13 with EnforcerRuleException

use of org.apache.maven.enforcer.rule.api.EnforcerRuleException in project metis-framework by europeana.

the class VocabularyCollectionMavenRule method execute.

@Override
public void execute(EnforcerRuleHelper enforcerRuleHelper) throws EnforcerRuleException {
    // Get the environment: the log and the project.
    final Log log = enforcerRuleHelper.getLog();
    final MavenProject project;
    try {
        project = enforcerRuleHelper.getComponent(MavenProject.class);
    } catch (ComponentLookupException e) {
        throw new EnforcerRuleException("Could not retrieve the project properties.", e);
    }
    // Get the vocabulary directory file
    final Path baseDirectory = project.getBasedir().toPath();
    final Path vocabularyDirectory = baseDirectory.resolve(vocabularyDirectoryFile);
    // Prepare validation
    final VocabularyCollectionImporter importer = new VocabularyCollectionImporterFactory().createImporter(baseDirectory, vocabularyDirectory);
    final VocabularyCollectionValidatorImpl validator = new VocabularyCollectionValidatorImpl(importer, lenientOnLackOfExamples, lenientOnMappingTestFailures, lenientOnExampleRetrievalFailures);
    log.info("");
    log.info("Validating vocabulary collection: " + importer.getDirectoryLocation().toString());
    // Perform validation
    try {
        validator.validate(vocabulary -> log.info("  Vocabulary found: " + vocabulary.getName()), log::warn);
    } catch (VocabularyImportException e) {
        log.error(e.getMessage());
        throw new EnforcerRuleException("Vocabulary collection validation failed.", e);
    }
    // Done
    log.info("Finished validating vocabulary collection.");
}
Also used : Path(java.nio.file.Path) MavenProject(org.apache.maven.project.MavenProject) Log(org.apache.maven.plugin.logging.Log) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) VocabularyImportException(eu.europeana.metis.dereference.vocimport.exception.VocabularyImportException)

Example 14 with EnforcerRuleException

use of org.apache.maven.enforcer.rule.api.EnforcerRuleException in project cloud-opensource-java by GoogleCloudPlatform.

the class LinkageCheckerRule method buildClassPathResult.

private static ClassPathResult buildClassPathResult(DependencyResolutionResult result) throws EnforcerRuleException {
    // The root node must have the project's JAR file
    DependencyNode root = result.getDependencyGraph();
    File rootFile = root.getArtifact().getFile();
    if (rootFile == null) {
        throw new EnforcerRuleException("The root project artifact is not associated with a file.");
    }
    List<Dependency> unresolvedDependencies = result.getUnresolvedDependencies();
    Set<Artifact> unresolvedArtifacts = unresolvedDependencies.stream().map(Dependency::getArtifact).collect(toImmutableSet());
    DependencyGraph dependencyGraph = DependencyGraph.from(root);
    AnnotatedClassPath annotatedClassPath = new AnnotatedClassPath();
    ImmutableList.Builder<UnresolvableArtifactProblem> problems = ImmutableList.builder();
    for (DependencyPath path : dependencyGraph.list()) {
        Artifact artifact = path.getLeaf();
        if (unresolvedArtifacts.contains(artifact)) {
            problems.add(new UnresolvableArtifactProblem(artifact));
        } else {
            annotatedClassPath.put(new ClassPathEntry(artifact), path);
        }
    }
    return new ClassPathResult(annotatedClassPath, problems.build());
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) Dependency(org.eclipse.aether.graph.Dependency) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) Artifact(org.eclipse.aether.artifact.Artifact) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) DependencyNode(org.eclipse.aether.graph.DependencyNode) UnresolvableArtifactProblem(com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem) File(java.io.File)

Example 15 with EnforcerRuleException

use of org.apache.maven.enforcer.rule.api.EnforcerRuleException in project cloud-opensource-java by GoogleCloudPlatform.

the class LinkageCheckerRule method findBomClasspath.

/**
 * Builds a class path for {@code bomProject}.
 */
private ClassPathResult findBomClasspath(MavenProject bomProject, RepositorySystemSession repositorySystemSession) throws EnforcerRuleException {
    ArtifactTypeRegistry artifactTypeRegistry = repositorySystemSession.getArtifactTypeRegistry();
    List<org.apache.maven.model.Dependency> managedDependencies = bomProject.getDependencyManagement().getDependencies();
    ImmutableList<Artifact> artifacts = managedDependencies.stream().map(dependency -> RepositoryUtils.toDependency(dependency, artifactTypeRegistry)).map(Dependency::getArtifact).filter(artifact -> !Bom.shouldSkipBomMember(artifact)).collect(toImmutableList());
    try {
        ClassPathResult result = classPathBuilder.resolve(artifacts, false, DependencyMediation.MAVEN);
        ImmutableList<UnresolvableArtifactProblem> artifactProblems = result.getArtifactProblems();
        if (!artifactProblems.isEmpty()) {
            throw new EnforcerRuleException("Failed to collect dependency: " + artifactProblems);
        }
        return result;
    } catch (InvalidVersionSpecificationException ex) {
        throw new EnforcerRuleException("Dependency mediation failed due to invalid version", ex);
    }
}
Also used : DependencyResolutionException(org.apache.maven.project.DependencyResolutionException) AndDependencySelector(org.eclipse.aether.util.graph.selector.AndDependencySelector) ExpressionEvaluationException(org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException) ArtifactTypeRegistry(org.eclipse.aether.artifact.ArtifactTypeRegistry) DependencyResolutionResult(org.apache.maven.project.DependencyResolutionResult) ExclusionDependencySelector(org.eclipse.aether.util.graph.selector.ExclusionDependencySelector) ClassReferenceGraph(com.google.cloud.tools.opensource.classpath.ClassReferenceGraph) NonTestDependencySelector(com.google.cloud.tools.opensource.dependencies.NonTestDependencySelector) DependencyMediation(com.google.cloud.tools.opensource.classpath.DependencyMediation) ClassPathBuilder(com.google.cloud.tools.opensource.classpath.ClassPathBuilder) MavenProject(org.apache.maven.project.MavenProject) Map(java.util.Map) ProjectDependenciesResolver(org.apache.maven.project.ProjectDependenciesResolver) LinkageProblemCauseAnnotator(com.google.cloud.tools.opensource.classpath.LinkageProblemCauseAnnotator) DefaultRepositoryCache(org.eclipse.aether.DefaultRepositoryCache) EnforcerRuleHelper(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper) Path(java.nio.file.Path) DependencyResolutionRequest(org.apache.maven.project.DependencyResolutionRequest) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) MojoExecution(org.apache.maven.plugin.MojoExecution) Set(java.util.Set) Artifact(org.eclipse.aether.artifact.Artifact) WARN(org.apache.maven.enforcer.rule.api.EnforcerLevel.WARN) AbstractNonCacheableEnforcerRule(org.apache.maven.plugins.enforcer.AbstractNonCacheableEnforcerRule) LinkageChecker(com.google.cloud.tools.opensource.classpath.LinkageChecker) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) List(java.util.List) RepositoryUtils(org.apache.maven.RepositoryUtils) UnresolvableArtifactProblem(com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem) Bom(com.google.cloud.tools.opensource.dependencies.Bom) OsProperties(com.google.cloud.tools.opensource.dependencies.OsProperties) Dependency(org.eclipse.aether.graph.Dependency) AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) HashMap(java.util.HashMap) LinkageProblem(com.google.cloud.tools.opensource.classpath.LinkageProblem) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) FilteringZipDependencySelector(com.google.cloud.tools.opensource.dependencies.FilteringZipDependencySelector) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) ImmutableList(com.google.common.collect.ImmutableList) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Nonnull(javax.annotation.Nonnull) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) MavenSession(org.apache.maven.execution.MavenSession) DependencyNode(org.eclipse.aether.graph.DependencyNode) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) IOException(java.io.IOException) Log(org.apache.maven.plugin.logging.Log) File(java.io.File) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) OptionalDependencySelector(org.eclipse.aether.util.graph.selector.OptionalDependencySelector) Paths(java.nio.file.Paths) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) VisibleForTesting(com.google.common.annotations.VisibleForTesting) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) DependencyGraphBuilder(com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) Dependency(org.eclipse.aether.graph.Dependency) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) Artifact(org.eclipse.aether.artifact.Artifact) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) ArtifactTypeRegistry(org.eclipse.aether.artifact.ArtifactTypeRegistry) UnresolvableArtifactProblem(com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem)

Aggregations

EnforcerRuleException (org.apache.maven.enforcer.rule.api.EnforcerRuleException)35 ExpressionEvaluationException (org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException)17 File (java.io.File)12 MavenProject (org.apache.maven.project.MavenProject)12 IOException (java.io.IOException)11 Artifact (org.apache.maven.artifact.Artifact)7 Log (org.apache.maven.plugin.logging.Log)6 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)6 Path (java.nio.file.Path)4 Matcher (java.util.regex.Matcher)4 DependencyResolutionResult (org.apache.maven.project.DependencyResolutionResult)4 DependencyNode (org.eclipse.aether.graph.DependencyNode)4 AnnotatedClassPath (com.google.cloud.tools.opensource.classpath.AnnotatedClassPath)3 ClassPathEntry (com.google.cloud.tools.opensource.classpath.ClassPathEntry)3 ClassPathResult (com.google.cloud.tools.opensource.classpath.ClassPathResult)3 DependencyGraph (com.google.cloud.tools.opensource.dependencies.DependencyGraph)3 DependencyPath (com.google.cloud.tools.opensource.dependencies.DependencyPath)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3