Search in sources :

Example 1 with ResolvedArtifactDependency

use of io.quarkus.maven.dependency.ResolvedArtifactDependency in project quarkus by quarkusio.

the class AppModelGradleResolver method resolveArtifact.

private ResolvedDependency resolveArtifact(ArtifactCoords appArtifact) throws AppModelResolverException {
    if (ResolvedDependency.class.isAssignableFrom(appArtifact.getClass())) {
        final ResolvedDependency resolved = (ResolvedDependency) appArtifact;
        if (resolved.isResolved()) {
            return resolved;
        }
    }
    final DefaultDependencyArtifact dep = new DefaultDependencyArtifact();
    dep.setExtension(appArtifact.getType());
    dep.setType(appArtifact.getType());
    dep.setName(appArtifact.getArtifactId());
    if (appArtifact.getClassifier() != null) {
        dep.setClassifier(appArtifact.getClassifier());
    }
    final DefaultExternalModuleDependency gradleDep = new DefaultExternalModuleDependency(appArtifact.getGroupId(), appArtifact.getArtifactId(), appArtifact.getVersion(), null);
    gradleDep.addArtifact(dep);
    final Configuration detachedConfig = project.getConfigurations().detachedConfiguration(gradleDep);
    final ResolvedConfiguration rc = detachedConfig.getResolvedConfiguration();
    Set<ResolvedArtifact> resolvedArtifacts;
    try {
        resolvedArtifacts = rc.getResolvedArtifacts();
    } catch (ResolveException e) {
        throw new AppModelResolverException("Failed to resolve " + appArtifact, e);
    }
    for (ResolvedArtifact a : resolvedArtifacts) {
        if (appArtifact.getArtifactId().equals(a.getName()) && appArtifact.getType().equals(a.getType()) && (a.getClassifier() == null ? appArtifact.getClassifier() == null : a.getClassifier().equals(appArtifact.getClassifier())) && appArtifact.getGroupId().equals(a.getModuleVersion().getId().getGroup())) {
            final String version = appArtifact.getVersion().equals(a.getModuleVersion().getId().getVersion()) ? appArtifact.getVersion() : a.getModuleVersion().getId().getVersion();
            return new ResolvedArtifactDependency(appArtifact.getGroupId(), appArtifact.getArtifactId(), appArtifact.getClassifier(), appArtifact.getType(), version, a.getFile().toPath());
        }
    }
    throw new AppModelResolverException("Failed to resolve " + appArtifact);
}
Also used : ResolveException(org.gradle.api.artifacts.ResolveException) ResolvedConfiguration(org.gradle.api.artifacts.ResolvedConfiguration) DefaultDependencyArtifact(org.gradle.api.internal.artifacts.dependencies.DefaultDependencyArtifact) ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) Configuration(org.gradle.api.artifacts.Configuration) ResolvedConfiguration(org.gradle.api.artifacts.ResolvedConfiguration) DefaultExternalModuleDependency(org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency) AppModelResolverException(io.quarkus.bootstrap.resolver.AppModelResolverException) ResolvedArtifactDependency(io.quarkus.maven.dependency.ResolvedArtifactDependency) ResolvedDependency(io.quarkus.maven.dependency.ResolvedDependency)

Example 2 with ResolvedArtifactDependency

use of io.quarkus.maven.dependency.ResolvedArtifactDependency in project quarkus by quarkusio.

the class JBangDevModeLauncherImpl method main.

public static Closeable main(String... args) {
    // avoid unknown config key warnings
    System.clearProperty("quarkus.dev");
    System.setProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager");
    URL url = JBangDevModeLauncherImpl.class.getClassLoader().getResource("jbang-dev.dat");
    String jarFilePath = url.getPath().substring(5, url.getPath().indexOf("!"));
    try (DataInputStream contextStream = new DataInputStream(JBangDevModeLauncherImpl.class.getClassLoader().getResourceAsStream("jbang-dev.dat"))) {
        String pomContents = contextStream.readUTF();
        Path sourceFile = Paths.get(contextStream.readUTF());
        int depCount = contextStream.readInt();
        Map<String, Path> deps = new HashMap<>();
        for (int i = 0; i < depCount; ++i) {
            String name = contextStream.readUTF();
            Path path = Paths.get(contextStream.readUTF());
            deps.put(name, path);
        }
        Path projectRoot = Files.createTempDirectory("quarkus-jbang");
        try (OutputStream out = Files.newOutputStream(projectRoot.resolve("pom.xml"))) {
            out.write(pomContents.getBytes(StandardCharsets.UTF_8));
        }
        Path targetClasses = projectRoot.resolve("target/classes");
        Files.createDirectories(targetClasses);
        try (ZipFile fz = new ZipFile(new File(jarFilePath))) {
            Enumeration<? extends ZipEntry> entries = fz.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                Path path = targetClasses.resolve(entry.getName()).normalize();
                if (!path.startsWith(targetClasses)) {
                    throw new IOException("Bad ZIP entry: " + path);
                }
                if (entry.isDirectory()) {
                    Files.createDirectories(path);
                } else {
                    Files.createDirectories(path.getParent());
                    Files.copy(fz.getInputStream(entry), path);
                    Files.setLastModifiedTime(path, entry.getLastModifiedTime());
                }
            }
        }
        Path srcDir = projectRoot.resolve("src/main/java");
        Files.createDirectories(srcDir);
        Files.createSymbolicLink(srcDir.resolve(sourceFile.getFileName().toString()), sourceFile);
        final LocalProject currentProject = LocalProject.loadWorkspace(projectRoot);
        final ResolvedDependency appArtifact = ResolvedDependencyBuilder.newInstance().setCoords(currentProject.getAppArtifact(ArtifactCoords.TYPE_JAR)).setResolvedPath(targetClasses).setWorkspaceModule(currentProject.toWorkspaceModule()).build();
        // todo : proper support for everything
        final QuarkusBootstrap.Builder builder = QuarkusBootstrap.builder().setBaseClassLoader(JBangDevModeLauncherImpl.class.getClassLoader()).setIsolateDeployment(true).setMode(QuarkusBootstrap.Mode.DEV).setTargetDirectory(targetClasses).setAppArtifact(appArtifact).setManagingProject(new GACTV("io.quarkus", "quarkus-bom", "", "pom", getQuarkusVersion())).setForcedDependencies(deps.entrySet().stream().map(s -> {
            String[] parts = s.getKey().split(":");
            Dependency artifact;
            if (parts.length == 3) {
                artifact = new ArtifactDependency(parts[0], parts[1], null, ArtifactCoords.TYPE_JAR, parts[2]);
            } else if (parts.length == 4) {
                artifact = new ArtifactDependency(parts[0], parts[1], null, parts[2], parts[3]);
            } else if (parts.length == 5) {
                artifact = new ArtifactDependency(parts[0], parts[1], parts[3], parts[2], parts[4]);
            } else {
                throw new RuntimeException("Invalid artifact " + s);
            }
            // artifact.setPath(s.getValue());
            return artifact;
        }).collect(Collectors.toList())).setApplicationRoot(targetClasses).setProjectRoot(projectRoot);
        Map<String, Object> context = new HashMap<>();
        context.put("app-project", currentProject);
        context.put("args", args);
        context.put("app-classes", targetClasses);
        final BootstrapMavenContext mvnCtx = new BootstrapMavenContext(BootstrapMavenContext.config().setCurrentProject(currentProject));
        final MavenArtifactResolver mvnResolver = new MavenArtifactResolver(mvnCtx);
        builder.setMavenArtifactResolver(mvnResolver);
        ((ResolvedArtifactDependency) currentProject.getAppArtifact(ArtifactCoords.TYPE_JAR)).setResolvedPaths(PathList.of(targetClasses));
        final CuratedApplication curatedApp = builder.build().bootstrap();
        final Object appInstance = curatedApp.runInAugmentClassLoader("io.quarkus.deployment.dev.IDEDevModeMain", context);
        return new JBangDevModeLauncherImpl(curatedApp, appInstance == null ? null : appInstance instanceof Closeable ? (Closeable) appInstance : null);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ResolvedArtifactDependency(io.quarkus.maven.dependency.ResolvedArtifactDependency) ArtifactDependency(io.quarkus.maven.dependency.ArtifactDependency) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) Closeable(java.io.Closeable) ResolvedDependency(io.quarkus.maven.dependency.ResolvedDependency) LocalProject(io.quarkus.bootstrap.resolver.maven.workspace.LocalProject) URL(java.net.URL) Path(java.nio.file.Path) BootstrapMavenContext(io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext) ResolvedArtifactDependency(io.quarkus.maven.dependency.ResolvedArtifactDependency) IOException(java.io.IOException) ResolvedArtifactDependency(io.quarkus.maven.dependency.ResolvedArtifactDependency) ArtifactDependency(io.quarkus.maven.dependency.ArtifactDependency) Dependency(io.quarkus.maven.dependency.Dependency) ResolvedDependency(io.quarkus.maven.dependency.ResolvedDependency) DataInputStream(java.io.DataInputStream) GACTV(io.quarkus.maven.dependency.GACTV) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) QuarkusBootstrap(io.quarkus.bootstrap.app.QuarkusBootstrap) MavenArtifactResolver(io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 3 with ResolvedArtifactDependency

use of io.quarkus.maven.dependency.ResolvedArtifactDependency in project quarkus by quarkusio.

the class JBangBuilderImpl method postBuild.

public static Map<String, Object> postBuild(Path appClasses, Path pomFile, List<Map.Entry<String, String>> repositories, List<Map.Entry<String, Path>> dependencies, boolean nativeImage) {
    final MavenArtifactResolver quarkusResolver;
    try {
        final BootstrapMavenContext mvnCtx = new BootstrapMavenContext(BootstrapMavenContext.config().setCurrentProject(pomFile.getParent().toString()));
        final List<RemoteRepository> remoteRepos = new ArrayList<>(mvnCtx.getRemoteRepositories());
        repositories.forEach(repo -> {
            remoteRepos.add(new RemoteRepository.Builder(repo.getKey(), "default", repo.getValue()).build());
        });
        quarkusResolver = MavenArtifactResolver.builder().setRepositorySystem(mvnCtx.getRepositorySystem()).setRepositorySystemSession(mvnCtx.getRepositorySystemSession()).setRemoteRepositoryManager(mvnCtx.getRemoteRepositoryManager()).setRemoteRepositories(remoteRepos).build();
    } catch (BootstrapMavenException e) {
        throw new IllegalStateException("Failed to initialize Quarkus bootstrap Maven resolver", e);
    }
    try {
        Path target = Files.createTempDirectory("quarkus-jbang");
        final ResolvedArtifactDependency appArtifact = new ResolvedArtifactDependency("dev.jbang.user", "quarkus", null, "jar", "999-SNAPSHOT", appClasses);
        final QuarkusBootstrap.Builder builder = QuarkusBootstrap.builder().setBaseClassLoader(JBangBuilderImpl.class.getClassLoader()).setMavenArtifactResolver(quarkusResolver).setProjectRoot(pomFile.getParent()).setTargetDirectory(target).setManagingProject(new GACTV("io.quarkus", "quarkus-bom", "", "pom", getQuarkusVersion())).setForcedDependencies(dependencies.stream().map(s -> {
            String[] parts = s.getKey().split(":");
            Dependency artifact;
            if (parts.length == 3) {
                artifact = new ArtifactDependency(parts[0], parts[1], null, ArtifactCoords.TYPE_JAR, parts[2]);
            } else if (parts.length == 4) {
                artifact = new ArtifactDependency(parts[0], parts[1], null, parts[2], parts[3]);
            } else if (parts.length == 5) {
                artifact = new ArtifactDependency(parts[0], parts[1], parts[3], parts[2], parts[4]);
            } else {
                throw new RuntimeException("Invalid artifact " + s.getKey());
            }
            // artifact.setPath(s.getValue());
            return artifact;
        }).collect(Collectors.toList())).setAppArtifact(appArtifact).setIsolateDeployment(true).setMode(QuarkusBootstrap.Mode.PROD);
        CuratedApplication app = builder.build().bootstrap();
        if (nativeImage) {
            System.setProperty("quarkus.package.type", "native");
        }
        Map<String, Object> output = new HashMap<>();
        app.runInAugmentClassLoader("io.quarkus.deployment.jbang.JBangAugmentorImpl", output);
        return output;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Path(java.nio.file.Path) CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Files(java.nio.file.Files) ResolvedArtifactDependency(io.quarkus.maven.dependency.ResolvedArtifactDependency) BootstrapMavenException(io.quarkus.bootstrap.resolver.maven.BootstrapMavenException) MavenArtifactResolver(io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver) IOException(java.io.IOException) HashMap(java.util.HashMap) BootstrapMavenContext(io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) List(java.util.List) QuarkusBootstrap(io.quarkus.bootstrap.app.QuarkusBootstrap) ArtifactCoords(io.quarkus.maven.dependency.ArtifactCoords) ArtifactDependency(io.quarkus.maven.dependency.ArtifactDependency) Map(java.util.Map) Dependency(io.quarkus.maven.dependency.Dependency) Path(java.nio.file.Path) GACTV(io.quarkus.maven.dependency.GACTV) InputStream(java.io.InputStream) BootstrapMavenContext(io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext) ResolvedArtifactDependency(io.quarkus.maven.dependency.ResolvedArtifactDependency) ArtifactDependency(io.quarkus.maven.dependency.ArtifactDependency) HashMap(java.util.HashMap) ResolvedArtifactDependency(io.quarkus.maven.dependency.ResolvedArtifactDependency) ArrayList(java.util.ArrayList) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) ResolvedArtifactDependency(io.quarkus.maven.dependency.ResolvedArtifactDependency) ArtifactDependency(io.quarkus.maven.dependency.ArtifactDependency) Dependency(io.quarkus.maven.dependency.Dependency) GACTV(io.quarkus.maven.dependency.GACTV) BootstrapMavenException(io.quarkus.bootstrap.resolver.maven.BootstrapMavenException) IOException(java.io.IOException) BootstrapMavenException(io.quarkus.bootstrap.resolver.maven.BootstrapMavenException) CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) QuarkusBootstrap(io.quarkus.bootstrap.app.QuarkusBootstrap) MavenArtifactResolver(io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver)

Aggregations

ResolvedArtifactDependency (io.quarkus.maven.dependency.ResolvedArtifactDependency)3 CuratedApplication (io.quarkus.bootstrap.app.CuratedApplication)2 QuarkusBootstrap (io.quarkus.bootstrap.app.QuarkusBootstrap)2 BootstrapMavenContext (io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext)2 MavenArtifactResolver (io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver)2 ArtifactDependency (io.quarkus.maven.dependency.ArtifactDependency)2 Dependency (io.quarkus.maven.dependency.Dependency)2 GACTV (io.quarkus.maven.dependency.GACTV)2 ResolvedDependency (io.quarkus.maven.dependency.ResolvedDependency)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 AppModelResolverException (io.quarkus.bootstrap.resolver.AppModelResolverException)1 BootstrapMavenException (io.quarkus.bootstrap.resolver.maven.BootstrapMavenException)1 LocalProject (io.quarkus.bootstrap.resolver.maven.workspace.LocalProject)1 ArtifactCoords (io.quarkus.maven.dependency.ArtifactCoords)1 Closeable (java.io.Closeable)1 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1