private static Artifact createDependencyArtifact( ArtifactFactory factory, Dependency dependency, String inheritedScope, ArtifactFilter inheritedFilter ) throws InvalidVersionSpecificationException { String effectiveScope = getEffectiveScope( dependency.getScope(), inheritedScope ); if ( effectiveScope == null ) { return null; } VersionRange versionRange = VersionRange.createFromVersionSpec( dependency.getVersion() ); Artifact dependencyArtifact = factory.createDependencyArtifact( dependency.getGroupId(), dependency.getArtifactId(), versionRange, dependency.getType(), dependency.getClassifier(), effectiveScope, dependency.isOptional() ); ArtifactFilter dependencyFilter = inheritedFilter; if ( dependencyFilter != null && !dependencyFilter.include( dependencyArtifact ) ) { return null; } if ( Artifact.SCOPE_SYSTEM.equals( effectiveScope ) ) { dependencyArtifact.setFile( new File( dependency.getSystemPath() ) ); } dependencyArtifact.setDependencyFilter( createDependencyFilter( dependency, dependencyFilter ) ); return dependencyArtifact; }
public static Artifact copyArtifact( Artifact artifact ) VersionRange range = artifact.getVersionRange(); range = VersionRange.createFromVersion( artifact.getVersion() ); DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), range.cloneOf(), artifact.getScope(), artifact.getType(), artifact.getClassifier(), artifact.getArtifactHandler(), artifact.isOptional() );
private boolean matchJdkVersionRange( String jdk ) throws InvalidVersionSpecificationException { VersionRange jdkVersionRange = VersionRange.createFromVersionSpec( convertJdkToMavenVersion( jdk ) ); DefaultArtifactVersion jdkVersion = new DefaultArtifactVersion( convertJdkToMavenVersion( getJdkVersion() ) ); return jdkVersionRange.containsVersion( jdkVersion ); }
@Override public boolean matches( String requirement ) { try { VersionRange range = VersionRange.createFromVersionSpec( requirement ); if ( range.hasRestrictions() ) { return range.containsVersion( version ); } else { return range.getRecommendedVersion().compareTo( version ) == 0; } } catch ( InvalidVersionSpecificationException ex ) { //TODO error reporting ex.printStackTrace(); return false; } }
restrictions = Collections.unmodifiableList( intersection( r1, r2 ) ); break; else if ( version == null && restriction.getRecommendedVersion() != null && r.containsVersion( restriction.getRecommendedVersion() ) ) version = restriction.getRecommendedVersion(); return new VersionRange( version, restrictions );
public static Artifact toArtifact( org.apache.maven.artifact.Artifact artifact ) { if ( artifact == null ) { return null; } String version = artifact.getVersion(); if ( version == null && artifact.getVersionRange() != null ) { version = artifact.getVersionRange().toString(); } Map<String, String> props = null; if ( org.apache.maven.artifact.Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) ) { String localPath = ( artifact.getFile() != null ) ? artifact.getFile().getPath() : ""; props = Collections.singletonMap( ArtifactProperties.LOCAL_PATH, localPath ); } Artifact result = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(), artifact.getArtifactHandler().getExtension(), version, props, newArtifactType( artifact.getType(), artifact.getArtifactHandler() ) ); result = result.setFile( artifact.getFile() ); return result; }
pomArtifact = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getScope() ); if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) ) DistributionManagement distMgmt = project.getDistributionManagement(); if ( distMgmt != null ) artifact.setGroupId( relocation.getGroupId() ); project.setGroupId( relocation.getGroupId() ); artifact.setArtifactId( relocation.getArtifactId() ); project.setArtifactId( relocation.getArtifactId() ); artifact.setVersionRange( VersionRange.createFromVersion( relocation.getVersion() ) ); project.setVersion( relocation.getVersion() );
private Artifact getTestNgArtifact() throws MojoExecutionException { Artifact artifact = getProjectArtifactMap().get( getTestNGArtifactName() ); Artifact projectArtifact = project.getArtifact(); String projectArtifactName = projectArtifact.getGroupId() + ":" + projectArtifact.getArtifactId(); if ( artifact != null ) { VersionRange range = createVersionRange(); if ( !range.containsVersion( new DefaultArtifactVersion( artifact.getVersion() ) ) ) { throw new MojoExecutionException( "TestNG support requires version 4.7 or above. You have declared version " + artifact.getVersion() ); } } else if ( projectArtifactName.equals( getTestNGArtifactName() ) ) { artifact = projectArtifact; } return artifact; }
public void execute() throws MojoExecutionException { if (skip) return; // capture the attached artifacts to sign before we start attaching our own stuff List<Artifact> attached = new ArrayList<Artifact>((List<Artifact>)project.getAttachedArtifacts()); PGPSecretKey secretKey = loadSecretKey(); Signer signer = new Signer(secretKey,loadPassPhrase(secretKey).toCharArray()); if ( !"pom".equals( project.getPackaging() ) ) sign(signer,project.getArtifact()); {// sign POM File pomToSign = new File( project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".pom" ); try { FileUtils.copyFile(project.getFile(), pomToSign); } catch ( IOException e ) { throw new MojoExecutionException( "Error copying POM for signing.", e ); } getLog().debug( "Generating signature for " + pomToSign ); // fake just enough Artifact for the sign method DefaultArtifact a = new DefaultArtifact(project.getGroupId(), project.getArtifactId(), VersionRange.createFromVersion(project.getVersion()), null, "pom", null, new DefaultArtifactHandler("pom")); a.setFile(pomToSign); sign(signer,a); } for (Artifact a : attached) sign(signer,a); }
private Artifact resolveArtifact( Artifact artifact ) throws MojoExecutionException, ArtifactNotFoundException { VersionRange versionRange; if ( artifact.getVersion() != null ) { versionRange = VersionRange.createFromVersion( artifact.getVersion() ); } else { versionRange = artifact.getVersionRange(); } /* * there's a bug with ArtifactFactory#createDependencyArtifact(String, String, VersionRange, * String, String, String) that ignores the scope parameter, that's why we use the one with * the extra null parameter */ Artifact resolvedArtifact = m_factory.createDependencyArtifact( artifact.getGroupId(), artifact.getArtifactId(), versionRange, artifact.getType(), artifact.getClassifier(), artifact.getScope(), null ); try { m_artifactResolver.resolve( resolvedArtifact, remoteRepositories, localRepository ); } catch ( ArtifactResolutionException e ) { throw new MojoExecutionException( "Error resolving artifact " + resolvedArtifact, e ); } return resolvedArtifact; }
private Artifact getComparisonArtifact(MavenParameters mavenParameters, PluginParameters pluginParameters) throws MojoFailureException, MojoExecutionException { VersionRange versionRange; try { versionRange = VersionRange.createFromVersionSpec(mavenParameters.getVersionRangeWithProjectVersion()); } catch (InvalidVersionSpecificationException e) { throw new MojoFailureException("Invalid version versionRange: " + e.getMessage(), e); try { MavenProject project = mavenParameters.getMavenProject(); previousArtifact = mavenParameters.getArtifactFactory().createDependencyArtifact(project.getGroupId(), project.getArtifactId(), versionRange, project.getPackaging(), null, Artifact.SCOPE_COMPILE); if (!previousArtifact.getVersionRange().isSelectedVersionKnown(previousArtifact)) { getLog().debug("Searching for versions in versionRange: " + previousArtifact.getVersionRange()); List<ArtifactVersion> availableVersions = mavenParameters.getMetadataSource().retrieveAvailableVersions(previousArtifact, mavenParameters.getLocalRepository(), project.getRemoteArtifactRepositories()); filterSnapshots(availableVersions); filterVersionPattern(availableVersions, pluginParameters); ArtifactVersion version = versionRange.matchVersion(availableVersions); if (version != null) { previousArtifact.selectVersion(version.toString()); throw new MojoFailureException("Invalid comparison version: " + e.getMessage(), e); } catch (ArtifactMetadataRetrievalException e) { throw new MojoExecutionException("Error determining previous version: " + e.getMessage(), e); if (previousArtifact.getVersion() == null) {
private File resolveArtifact(String artifactSpec, File dir) throws MojoExecutionException { try { Properties detectorProps = new Properties(); new PlatformDetector().detect(detectorProps, null); String platform = detectorProps.getProperty("os.detected.classifier"); getLog().info("Resolving artifact: " + artifactSpec + ", platform: " + platform); String[] as = artifactSpec.split(":"); Artifact artifact = artifactFactory.createDependencyArtifact(as[0], as[1], VersionRange.createFromVersionSpec(as[2]), "exe", platform, Artifact.SCOPE_RUNTIME); artifactResolver.resolve(artifact, remoteRepositories, localRepository); File tempFile = File.createTempFile(as[1], ".exe", dir); copyFile(artifact.getFile(), tempFile); tempFile.setExecutable(true); tempFile.deleteOnExit(); return tempFile; } catch (Exception e) { throw new MojoExecutionException("Error resolving artifact: " + artifactSpec, e); } }
static boolean isWithinVersionSpec( @Nullable Artifact artifact, @Nonnull String versionSpec ) { if ( artifact == null ) { return false; } try { VersionRange range = createFromVersionSpec( versionSpec ); try { return range.containsVersion( artifact.getSelectedVersion() ); } catch ( NullPointerException e ) { return range.containsVersion( new DefaultArtifactVersion( artifact.getBaseVersion() ) ); } } catch ( InvalidVersionSpecificationException | OverConstrainedVersionException e ) { throw new RuntimeException( "Bug in plugin. Please report with stacktrace" ); } }
protected Artifact getArtifact(ArtifactItem artifactItem) throws MojoExecutionException, InvalidVersionSpecificationException { Artifact artifact; VersionRange vr = VersionRange.createFromVersionSpec(artifactItem.getVersion()); if (StringUtils.isEmpty(artifactItem.getClassifier())) { artifact = factory.createDependencyArtifact( artifactItem.getGroupId(), artifactItem.getArtifactId(), vr, artifactItem.getType(), null, Artifact.SCOPE_COMPILE ); } else { artifact = factory.createDependencyArtifact( artifactItem.getGroupId(), artifactItem.getArtifactId(), vr, artifactItem.getType(), artifactItem.getClassifier(), Artifact.SCOPE_COMPILE ); } try { resolver.resolve(artifact, remoteRepos, local); } catch (ArtifactResolutionException | ArtifactNotFoundException e) { throw new MojoExecutionException("Error resolving artifact "+artifact, e); } return artifact; }
if ( StringUtils.isEmpty( artifact.getVersion() ) ) versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION ); versionRange = VersionRange.createFromVersion( artifact.getVersion() ); Artifact pluginArtifact = artifactFactory.createParentArtifact( artifact.getGroupId(), artifact .getArtifactId(), versionRange.toString() ); List<?> artifactRepositories = project.getPluginArtifactRepositories(); if ( artifactRepositories == null ) artifactRepositories, localRepository ); tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(), pluginProject .getVersion(), pluginProject.getUrl() ) );
private Collection<Artifact> getAllDependencies() throws MojoExecutionException { List<Artifact> artifacts = new ArrayList<>(); for (Iterator<?> dependencies = project.getDependencies().iterator(); dependencies.hasNext();) { Dependency dependency = (Dependency)dependencies.next(); versionRange = VersionRange.createFromVersionSpec(dependency.getVersion()); } catch (InvalidVersionSpecificationException e) { throw new MojoExecutionException("unable to parse version", e); Artifact art = this.artifactFactory.createDependencyArtifact(groupId, artifactId, versionRange, type, classifier, scope, null, optional); art.setFile(new File(dependency.getSystemPath())); art.setDependencyFilter(newFilter);
public Artifact createDependency(final Artifact query) { assert query != null; return artifactFactory.createDependencyArtifact( query.getGroupId(), query.getArtifactId(), VersionRange.createFromVersion(query.getVersion()), "jar", null, Artifact.SCOPE_RUNTIME); }
public Artifact create( MavenProject project ) { ArtifactHandler handler = getArtifactHandlerManager().getArtifactHandler( project.getPackaging() ); return new DefaultArtifact( project.getGroupId(), project.getArtifactId(), VersionRange.createFromVersion( project.getVersion() ), null, project.getPackaging(), null, handler, false ); }
artifactFactory.createDependencyArtifact( "org.codehaus.mojo.appassembler", "appassembler-booter", VersionRange.createFromVersion( pluginVersion ), "jar", null, Artifact.SCOPE_RUNTIME ); try artifactFactory.createBuildArtifact( "org.codehaus.mojo.appassembler", "appassembler-maven-plugin", pluginVersion, "jar" ); throw new MojoExecutionException( "Failed to copy artifact.", e ); throw new MojoExecutionException( "Failed to copy artifact.", e );
buildRequest.setRemoteRepositories( project.getPluginArtifactRepositories() ); if ( StringUtils.isEmpty( plugin.getVersion() ) ) versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION ); versionRange = VersionRange.createFromVersion( plugin.getVersion() ); .getArtifactId(), versionRange.toString() ); tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(), pluginProject .getVersion(), pluginProject.getUrl() ) );