Search in sources :

Example 1 with SimpleLogger

use of org.wildfly.swarm.spi.meta.SimpleLogger in project wildfly-swarm by wildfly-swarm.

the class PackageMojo method executeSpecific.

@SuppressWarnings("deprecation")
@Override
public void executeSpecific() throws MojoExecutionException, MojoFailureException {
    if (this.skip) {
        getLog().info("Skipping packaging");
        return;
    }
    if (this.project.getPackaging().equals("pom")) {
        getLog().info("Not processing project with pom packaging");
        return;
    }
    initProperties(false);
    final Artifact primaryArtifact = this.project.getArtifact();
    final String finalName = this.project.getBuild().getFinalName();
    final String type = primaryArtifact.getType();
    final File primaryArtifactFile = divineFile();
    if (primaryArtifactFile == null) {
        throw new MojoExecutionException("Cannot package without a primary artifact; please `mvn package` prior to invoking wildfly-swarm:package from the command-line");
    }
    final DeclaredDependencies declaredDependencies = new DeclaredDependencies();
    final BuildTool tool = new BuildTool(mavenArtifactResolvingHelper()).projectArtifact(primaryArtifact.getGroupId(), primaryArtifact.getArtifactId(), primaryArtifact.getBaseVersion(), type, primaryArtifactFile, finalName.endsWith("." + type) ? finalName : String.format("%s.%s", finalName, type)).properties(this.properties).mainClass(this.mainClass).bundleDependencies(this.bundleDependencies).executable(executable).executableScript(executableScript).fractionDetectionMode(fractionDetectMode).hollow(hollow).logger(new SimpleLogger() {

        @Override
        public void debug(String msg) {
            getLog().debug(msg);
        }

        @Override
        public void info(String msg) {
            getLog().info(msg);
        }

        @Override
        public void error(String msg) {
            getLog().error(msg);
        }

        @Override
        public void error(String msg, Throwable t) {
            getLog().error(msg, t);
        }
    });
    this.additionalFractions.stream().map(f -> FractionDescriptor.fromGav(FractionList.get(), f)).map(ArtifactSpec::fromFractionDescriptor).forEach(tool::fraction);
    Map<ArtifactSpec, Set<ArtifactSpec>> buckets = createBuckets(this.project.getArtifacts(), this.project.getDependencies());
    for (ArtifactSpec directDep : buckets.keySet()) {
        if (!(directDep.scope.equals("compile") || directDep.scope.equals("runtime"))) {
            // ignore anything but compile and runtime
            continue;
        }
        Set<ArtifactSpec> transientDeps = buckets.get(directDep);
        if (transientDeps.isEmpty()) {
            declaredDependencies.add(directDep);
        } else {
            for (ArtifactSpec transientDep : transientDeps) {
                declaredDependencies.add(directDep, transientDep);
            }
        }
    }
    tool.declaredDependencies(declaredDependencies);
    this.project.getResources().forEach(r -> tool.resourceDirectory(r.getDirectory()));
    Path uberjarResourcesDir = null;
    if (this.uberjarResources == null) {
        uberjarResourcesDir = Paths.get(this.project.getBasedir().toString()).resolve("src").resolve("main").resolve("uberjar");
    } else {
        uberjarResourcesDir = Paths.get(this.uberjarResources);
    }
    tool.uberjarResourcesDirectory(uberjarResourcesDir);
    this.additionalModules.stream().map(m -> new File(this.project.getBuild().getOutputDirectory(), m)).filter(File::exists).map(File::getAbsolutePath).forEach(tool::additionalModule);
    try {
        File jar = tool.build(finalName + (this.hollow ? "-hollow" : ""), Paths.get(this.projectBuildDir));
        ArtifactHandler handler = new DefaultArtifactHandler("jar");
        Artifact swarmJarArtifact = new DefaultArtifact(primaryArtifact.getGroupId(), primaryArtifact.getArtifactId(), primaryArtifact.getBaseVersion(), primaryArtifact.getScope(), "jar", (this.hollow ? "hollow" : "") + "swarm", handler);
        swarmJarArtifact.setFile(jar);
        this.project.addAttachedArtifact(swarmJarArtifact);
        if (this.project.getPackaging().equals("war")) {
            tool.repackageWar(primaryArtifactFile);
        }
    } catch (Exception e) {
        throw new MojoFailureException("Unable to create -swarm.jar", e);
    }
}
Also used : Path(java.nio.file.Path) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) SimpleLogger(org.wildfly.swarm.spi.meta.SimpleLogger) ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) Files(java.nio.file.Files) DeclaredDependencies(org.wildfly.swarm.tools.DeclaredDependencies) Set(java.util.Set) FractionDescriptor(org.wildfly.swarm.fractions.FractionDescriptor) BuildTool(org.wildfly.swarm.tools.BuildTool) Parameter(org.apache.maven.plugins.annotations.Parameter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) FractionList(org.wildfly.swarm.fractions.FractionList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Mojo(org.apache.maven.plugins.annotations.Mojo) Paths(java.nio.file.Paths) Map(java.util.Map) Artifact(org.apache.maven.artifact.Artifact) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) ResolutionScope(org.apache.maven.plugins.annotations.ResolutionScope) Path(java.nio.file.Path) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) Set(java.util.Set) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DeclaredDependencies(org.wildfly.swarm.tools.DeclaredDependencies) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) Artifact(org.apache.maven.artifact.Artifact) SimpleLogger(org.wildfly.swarm.spi.meta.SimpleLogger) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) BuildTool(org.wildfly.swarm.tools.BuildTool) File(java.io.File) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact)

Example 2 with SimpleLogger

use of org.wildfly.swarm.spi.meta.SimpleLogger in project wildfly-swarm by wildfly-swarm.

the class PackageTask method packageForSwarm.

@TaskAction
public void packageForSwarm() throws Exception {
    final Project project = getProject();
    GradleArtifactResolvingHelper resolvingHelper = new GradleArtifactResolvingHelper(project);
    Properties propertiesFromExtension = getPropertiesFromExtension();
    List<File> moduleDirs = getModuleDirs();
    if (moduleDirs.isEmpty()) {
        Path resourcesOutputDir = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().findByName(SourceSet.MAIN_SOURCE_SET_NAME).getOutput().getResourcesDir().toPath().resolve(MODULE_DIR_NAME);
        if (Files.isDirectory(resourcesOutputDir)) {
            File moduleDir = resourcesOutputDir.toFile();
            moduleDirs.add(moduleDir);
        }
    }
    this.tool = new BuildTool(resolvingHelper).projectArtifact(this.jarTask.getGroup().toString(), this.jarTask.getBaseName(), this.jarTask.getVersion(), getPackaging(), getProjectArtifactFile()).mainClass(getMainClassName()).bundleDependencies(getBundleDependencies()).executable(getExecutable()).executableScript(getExecutableScript()).properties(propertiesFromExtension).properties(getPropertiesFromFile()).properties(PropertiesUtil.filteredSystemProperties(propertiesFromExtension, false)).fractionDetectionMode(getSwarmExtension().getFractionDetectMode()).hollow(getHollow()).additionalModules(moduleDirs.stream().filter(File::exists).map(File::getAbsolutePath).collect(Collectors.toList())).logger(new SimpleLogger() {

        @Override
        public void debug(String msg) {
            getLogger().debug(msg);
        }

        @Override
        public void info(String msg) {
            getLogger().info(msg);
        }

        @Override
        public void error(String msg) {
            getLogger().error(msg);
        }

        @Override
        public void error(String msg, Throwable t) {
            getLogger().error(msg, t);
        }
    });
    DeclaredDependencies declaredDependencies = new DeclaredDependencies();
    List<ArtifactSpec> explicitDependencies = new ArrayList<>();
    /* project.getConfigurations()
                .getByName("compile")
                .getAllDependencies()
                .forEach((artifact) -> {
                    String groupId = artifact.getGroup();
                    String artifactId = artifact.getName();
                    explicitDependencies.add(new ArtifactSpec("compile", groupId, artifactId, null, "jar", null, null));
                });

        project.getConfigurations()
                .getByName("compile")
                .getResolvedConfiguration()
                .getResolvedArtifacts()
                .forEach(e -> addDependency(declaredDependencies, explicitDependencies, e));*/
    ResolvedConfiguration resolvedConfiguration = project.getConfigurations().getByName("default").getResolvedConfiguration();
    Set<ResolvedDependency> directDeps = resolvedConfiguration.getFirstLevelModuleDependencies();
    for (ResolvedDependency directDep : directDeps) {
        assert directDep.getModuleArtifacts().iterator().hasNext() : "Expected module artifacts";
        ArtifactSpec parent = new ArtifactSpec("compile", directDep.getModule().getId().getGroup(), directDep.getModule().getId().getName(), directDep.getModule().getId().getVersion(), directDep.getModuleArtifacts().iterator().next().getExtension(), null, null);
        Set<ArtifactSpec> artifactSpecs = resolvingHelper.resolveAll(new HashSet<>(Collections.singletonList(parent)));
        artifactSpecs.forEach(a -> declaredDependencies.add(parent, a));
    }
    tool.declaredDependencies(declaredDependencies);
    final Boolean bundleDependencies = getBundleDependencies();
    if (bundleDependencies != null) {
        this.tool.bundleDependencies(bundleDependencies);
    }
    this.tool.build(getBaseName(), getOutputDirectory());
}
Also used : Path(java.nio.file.Path) DeclaredDependencies(org.wildfly.swarm.tools.DeclaredDependencies) ArrayList(java.util.ArrayList) ResolvedDependency(org.gradle.api.artifacts.ResolvedDependency) Properties(java.util.Properties) SimpleLogger(org.wildfly.swarm.spi.meta.SimpleLogger) Project(org.gradle.api.Project) ResolvedConfiguration(org.gradle.api.artifacts.ResolvedConfiguration) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) BuildTool(org.wildfly.swarm.tools.BuildTool) File(java.io.File) OutputFile(org.gradle.api.tasks.OutputFile) InputFile(org.gradle.api.tasks.InputFile) TaskAction(org.gradle.api.tasks.TaskAction)

Aggregations

File (java.io.File)2 Path (java.nio.file.Path)2 SimpleLogger (org.wildfly.swarm.spi.meta.SimpleLogger)2 ArtifactSpec (org.wildfly.swarm.tools.ArtifactSpec)2 BuildTool (org.wildfly.swarm.tools.BuildTool)2 DeclaredDependencies (org.wildfly.swarm.tools.DeclaredDependencies)2 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Set (java.util.Set)1 Artifact (org.apache.maven.artifact.Artifact)1 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)1 ArtifactHandler (org.apache.maven.artifact.handler.ArtifactHandler)1 DefaultArtifactHandler (org.apache.maven.artifact.handler.DefaultArtifactHandler)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 LifecyclePhase (org.apache.maven.plugins.annotations.LifecyclePhase)1 Mojo (org.apache.maven.plugins.annotations.Mojo)1