Java Code Examples for org.eclipse.aether.artifact.Artifact#setFile()

The following examples show how to use org.eclipse.aether.artifact.Artifact#setFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AetherUtils.java    From takari-lifecycle with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: MavenLocalRepositoryManager.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRequest request) {
    final LocalArtifactResult result = delegate.find(session, request);
    if (result.isAvailable()) {
        return result;
    }
    final Artifact artifact = request.getArtifact();
    final Path userRepoPath = getLocalPath(userLocalRepo, artifact.getGroupId(), artifact.getArtifactId(),
            artifact.getClassifier(), artifact.getExtension(), artifact.getVersion());
    if (!Files.exists(userRepoPath)) {
        return result;
    }
    if (relinkResolvedArtifacts) {
        final Path creatorRepoPath = getLocalPath(appCreatorRepo, artifact.getGroupId(), artifact.getArtifactId(),
                artifact.getClassifier(), artifact.getExtension(), artifact.getVersion());
        try {
            IoUtils.copy(userRepoPath, creatorRepoPath);
        } catch (IOException e) {
            throw new IllegalStateException("Failed to copy " + userRepoPath + " to a staging repo", e);
        }
        result.setFile(creatorRepoPath.toFile());
    } else {
        result.setFile(userRepoPath.toFile());
    }
    artifact.setFile(result.getFile());
    result.setAvailable(true);
    return result;
}
 
Example 3
Source File: ComponentDependenciesBase.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
protected Artifact resolve(final Artifact dep, final String classifier, final String type) {
    final LocalRepositoryManager lrm = repositorySystemSession.getLocalRepositoryManager();
    final Artifact artifact =
            new DefaultArtifact(dep.getGroupId(), dep.getArtifactId(), classifier, type, getVersion(dep));
    final File location = new File(lrm.getRepository().getBasedir(), lrm.getPathForLocalArtifact(artifact));
    if (!location.exists()) {
        return resolve(artifact);
    }
    return artifact.setFile(location);
}
 
Example 4
Source File: MavenTest.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testMvn1 () throws Exception
{
    final ChannelTester ct = ChannelTester.create ( getWebContext (), "m1" );
    ct.addAspect ( "mvn" );
    ct.addAspect ( "maven.repo" );
    ct.assignDeployGroup ( "m1" );

    final String key = ct.getDeployKeys ().iterator ().next (); // get first

    assertNotNull ( key );

    final RepositorySystem system = MavenUtil.newRepositorySystem ();
    final RepositorySystemSession session = MavenUtil.newRepositorySystemSession ( system );

    Artifact jarArtifact = new DefaultArtifact ( "org.eclipse.packagedrone.testing", "test.felix1", "", "jar", "0.0.1-SNAPSHOT" );
    jarArtifact = jarArtifact.setFile ( new File ( TEST_1_JAR ) );

    Artifact pomArtifact = new SubArtifact ( jarArtifact, "", "pom" );
    pomArtifact = pomArtifact.setFile ( new File ( TEST_1_POM ) );

    Artifact srcArtifact = new SubArtifact ( jarArtifact, "sources", "jar" );
    srcArtifact = srcArtifact.setFile ( new File ( TEST_1_SOURCES_JAR ) );

    final AuthenticationBuilder ab = new AuthenticationBuilder ();
    ab.addUsername ( "deploy" );
    ab.addPassword ( key );
    final Authentication auth = ab.build ();
    final RemoteRepository distRepo = new RemoteRepository.Builder ( "test", "default", resolve ( String.format ( "/maven/%s", ct.getId () ) ) ).setAuthentication ( auth ).build ();

    final DeployRequest deployRequest = new DeployRequest ();
    deployRequest.addArtifact ( jarArtifact ).addArtifact ( pomArtifact ).addArtifact ( srcArtifact );
    deployRequest.setRepository ( distRepo );

    system.deploy ( session, deployRequest );

    testUrl ( String.format ( "/maven/%s", ct.getId () ) ); // index page

    // FIXME: check more data
}
 
Example 5
Source File: Analyzer.java    From revapi with Apache License 2.0 5 votes vote down vote up
private static Artifact findProjectArtifact(MavenProject project) {
    String extension = project.getArtifact().getArtifactHandler().getExtension();

    String fileName = project.getModel().getBuild().getFinalName() + "." + extension;
    File f = new File(new File(project.getBasedir(), "target"), fileName);
    if (f.exists()) {
        Artifact ret = RepositoryUtils.toArtifact(project.getArtifact());
        return ret.setFile(f);
    } else {
        return null;
    }
}
 
Example 6
Source File: OMetadataTest.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void init() {
    OrienteerClassLoaderUtil.deleteMetadataFile();
    Artifact artifact = new DefaultArtifact("org.company:artifact:1.0");
    artifact = artifact.setFile(new File("module.jar"));
    metadata = new OArtifact();
    metadata.setArtifactReference(OArtifactReference.valueOf(artifact));
    metadata.setLoad(true);
    metadata.setTrusted(true);
    OrienteerClassLoaderUtil.createOArtifactsMetadata(Lists.newArrayList(metadata));
    OArtifact oArtifact = OrienteerClassLoaderUtil.getOArtifactsMetadataAsList().get(0);
    testArtifact(metadata, oArtifact);
}
 
Example 7
Source File: OMetadataTest.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Test
public void addListAndDeleteToMetadata() throws Exception {
    Artifact artifact1 = new DefaultArtifact("org.orienteer:orienteer-core:1.3-SNAPSHOT");
    artifact1 = artifact1.setFile(new File("orienteer-core.jar"));
    OArtifact oArtifact1 = new OArtifact();
    oArtifact1.setArtifactReference(OArtifactReference.valueOf(artifact1));
    oArtifact1.setLoad(true);
    oArtifact1.setTrusted(true);

    Artifact artifact2 = new DefaultArtifact("org.orienteer:devutils:1.3-SNAPSHOT");
    artifact2 = artifact2.setFile(new File("orienteer-devutils.jar"));
    OArtifact oArtifact2 = new OArtifact();
    oArtifact2.setArtifactReference(OArtifactReference.valueOf(artifact2));
    oArtifact2.setLoad(true);
    oArtifact2.setTrusted(true);
    List<OArtifact> list = Lists.newArrayList(metadata, oArtifact1, oArtifact2);
    OrienteerClassLoaderUtil.updateOArtifactsJarsInMetadata(list);
    List<OArtifact> result = OrienteerClassLoaderUtil.getOArtifactsMetadataAsList();

    testArtifact(metadata, result.get(0));
    testArtifact(oArtifact1, result.get(1));
    testArtifact(oArtifact2, result.get(2));

    OrienteerClassLoaderUtil.deleteOArtifactsFromMetadata(Lists.newArrayList(oArtifact1, oArtifact2));
    List<OArtifact> metadataAsList = OrienteerClassLoaderUtil.getOArtifactsMetadataAsList();

    assertTrue("metadata size must be 1", metadataAsList.size() == 1);
}
 
Example 8
Source File: MavenPluginUtil.java    From galleon with Apache License 2.0 4 votes vote down vote up
public InstallRequest getInstallLayoutRequest(final Path layoutDir, final MavenProject project) throws IOException {
    final File pomFile = project.getFile();
    final Logger logger = getLogger();
    final InstallRequest installReq = new InstallRequest();
    try (DirectoryStream<Path> wdStream = Files.newDirectoryStream(layoutDir, entry -> Files.isDirectory(entry))) {
        for (Path groupDir : wdStream) {
            final String groupId = groupDir.getFileName().toString();
            try (DirectoryStream<Path> groupStream = Files.newDirectoryStream(groupDir)) {
                for (Path artifactDir : groupStream) {
                    final String artifactId = artifactDir.getFileName().toString();
                    try (DirectoryStream<Path> artifactStream = Files.newDirectoryStream(artifactDir)) {
                        for (Path versionDir : artifactStream) {
                            if(logger.isDebugEnabled()) {
                                logger.debug("Preparing feature-pack " + versionDir.toAbsolutePath());
                            }
                            final Path zippedFP = layoutDir.resolve(
                                    groupId + '_' + artifactId + '_' + versionDir.getFileName().toString() + ".zip");
                            if(Files.exists(zippedFP)) {
                                IoUtils.recursiveDelete(zippedFP);
                            }
                            ZipUtils.zip(versionDir, zippedFP);
                            final Artifact artifact = new DefaultArtifact(
                                    groupDir.getFileName().toString(),
                                    artifactDir.getFileName().toString(), null,
                                    "zip", versionDir.getFileName().toString(), null, zippedFP.toFile());
                            Path target = Paths.get(project.getBuild().getDirectory()).resolve(project.getBuild().getFinalName() + ".zip");
                            IoUtils.copy(zippedFP, target);
                            installReq.addArtifact(artifact);
                            if (pomFile != null && Files.exists(pomFile.toPath())) {
                                Artifact pomArtifact = new SubArtifact(artifact, "", "pom");
                                pomArtifact = pomArtifact.setFile(pomFile);
                                installReq.addArtifact(pomArtifact);
                            }
                        }
                    }
                }
            }
        }
    }
    return installReq;
}
 
Example 9
Source File: NbArtifactFixer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public @Override File resolve(Artifact artifact) {
    if (!artifact.getExtension().equals(NbMavenProject.TYPE_POM)) {
        return null;
    }
    if (!artifact.getClassifier().isEmpty()) {
        return null;
    }
    ArtifactRepository local = EmbedderFactory.getProjectEmbedder().getLocalRepository();
    if (local.getLayout() != null) { // #189807: for unknown reasons, there is no layout when running inside MavenCommandLineExecutor.run
        
        //the special snapshot handling is important in case of SNAPSHOT or x-SNAPSHOT versions, for some reason aether has slightly different
        //handling of baseversion compared to maven artifact. we need to manually set the baseversion here..
        boolean isSnapshot = artifact.isSnapshot();
        DefaultArtifact art = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), null, artifact.getExtension(), artifact.getClassifier(), new DefaultArtifactHandler(artifact.getExtension()));
        if (isSnapshot) {
            art.setBaseVersion(artifact.getBaseVersion());
        }
        String path = local.pathOf(art);
        if (new File(local.getBasedir(), path).exists()) {
            return null; // for now, we prefer the repository version when available
        }
    }
    //#234586
    Set<String> gavSet = gav.get();
    if (gavSet == null) {
        gavSet = new HashSet<String>();
        gav.set(gavSet);
    }
    String id = artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion();

    if (!gavSet.contains(id)) {
        try {
            gavSet.add(id); //#234586
            File pom = MavenFileOwnerQueryImpl.getInstance().getOwnerPOM(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
            if (pom != null) {
                //instead of workarounds down the road, we set the artifact's file here.
                // some stacktraces to maven/aether do set it after querying our code, but some don't for reasons unknown to me.
                artifact.setFile(pom);
                return pom;
            }
        } finally {
            gavSet.remove(id); //#234586
            if (gavSet.isEmpty()) {
                gav.remove();
            }
        }
    } else {
        LOG.log(Level.INFO, "Cycle in NbArtifactFixer resolution (issue #234586): {0}", Arrays.toString(gavSet.toArray()));
    }
    
    try {
        File f = createFallbackPOM(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
        //instead of workarounds down the road, we set the artifact's file here.
        // some stacktraces to maven/aether do set it after querying our code, but some don't for reasons unknown to me.
        artifact.setFile(f);
        return f;
    } catch (IOException x) {
        Exceptions.printStackTrace(x);
        return null;
    }
}
 
Example 10
Source File: OArtifactReference.java    From Orienteer with Apache License 2.0 4 votes vote down vote up
public Artifact toAetherArtifact() {
    Artifact result = new DefaultArtifact(String.format("%s:%s:jar:%s", groupId, artifactId,version));
    return result.setFile(file);
}
 
Example 11
Source File: ClasspathWorkspaceReader.java    From furnace with Eclipse Public License 1.0 4 votes vote down vote up
private Artifact createFoundArtifact(final File pomFile)
{
   try
   {
      if (log.isLoggable(Level.FINE))
      {
         log.fine("Processing " + pomFile.getAbsolutePath() + " for classpath artifact resolution");
      }
      Xpp3Dom dom = null;
      try (FileReader reader = new FileReader(pomFile))
      {
         dom = Xpp3DomBuilder.build(reader);
      }
      Xpp3Dom groupIdNode = dom.getChild("groupId");
      String groupId = (groupIdNode == null) ? null : groupIdNode.getValue();
      String artifactId = dom.getChild("artifactId").getValue();
      Xpp3Dom packaging = dom.getChild("packaging");
      String type = (packaging == null) ? "jar" : packaging.getValue();
      Xpp3Dom versionNode = dom.getChild("version");
      String version = (versionNode == null) ? null : versionNode.getValue();

      if (groupId == null || groupId.isEmpty())
      {
         groupId = dom.getChild("parent").getChild("groupId").getValue();
      }
      if (type == null || type.isEmpty())
      {
         type = "jar";
      }
      if (version == null || version.isEmpty())
      {
         version = dom.getChild("parent").getChild("version").getValue();
      }

      final Artifact foundArtifact = new DefaultArtifact(groupId, artifactId, type, version);
      foundArtifact.setFile(pomFile);
      return foundArtifact;
   }
   catch (final Exception e)
   {
      throw new RuntimeException("Could not parse pom.xml: " + pomFile, e);
   }
}