Search in sources :

Example 6 with DeclaredDependencies

use of org.wildfly.swarm.tools.DeclaredDependencies in project wildfly-swarm by wildfly-swarm.

the class Main method generateSwarmJar.

protected static File generateSwarmJar(final String[] args) throws Exception {
    OptionSet foundOptions = null;
    try {
        foundOptions = OPT_PARSER.parse(args);
    } catch (OptionException e) {
        exit(e.getMessage(), true);
    }
    if (foundOptions.has(HELP_OPT)) {
        exit(null, 0, true);
    }
    if (foundOptions.has(VERSION_OPT)) {
        exit("swarmtool v" + VERSION, 0);
    }
    final List<File> nonOptArgs = foundOptions.valuesOf(SOURCE_OPT);
    if (nonOptArgs.isEmpty()) {
        exit("No source artifact specified.", true);
    }
    if (nonOptArgs.size() > 1) {
        exit("Too many source artifacts provided (" + nonOptArgs + ")", true);
    }
    final File source = nonOptArgs.get(0);
    if (!source.exists()) {
        exit("File " + source.getAbsolutePath() + " does not exist.");
    }
    final Properties properties = new Properties();
    if (foundOptions.has(SYSPROPS_FILE_OPT)) {
        try (InputStream in = new FileInputStream(foundOptions.valueOf(SYSPROPS_FILE_OPT))) {
            properties.load(in);
        }
    }
    foundOptions.valuesOf(SYSPROPS_OPT).forEach(prop -> {
        final String[] parts = prop.split("=");
        properties.put(parts[0], parts[1]);
    });
    final DeclaredDependencies dependencies = new DeclaredDependencies();
    foundOptions.valuesOf(DEPENDENCIES_OPT).stream().map(DeclaredDependencies::createSpec).forEach(dependencies::add);
    final String[] parts = source.getName().split("\\.(?=[^\\.]+$)");
    final String baseName = parts[0];
    final String type = parts[1] == null ? "jar" : parts[1];
    final String jarName = foundOptions.has(NAME_OPT) ? foundOptions.valueOf(NAME_OPT) : baseName;
    final String outDir = new File(foundOptions.valueOf(OUTPUT_DIR_OPT)).getCanonicalPath();
    final String suffix = foundOptions.has(HOLLOW_OPT) ? "-hollow-swarm" : "-swarm";
    final BuildTool tool = new BuildTool(getResolvingHelper(foundOptions.valuesOf(REPOS_OPT))).projectArtifact("", baseName, "", type, source).declaredDependencies(dependencies).fractionDetectionMode(foundOptions.has(DISABLE_AUTO_DETECT_OPT) ? BuildTool.FractionDetectionMode.never : BuildTool.FractionDetectionMode.force).bundleDependencies(!foundOptions.has(DISABLE_BUNDLE_DEPS_OPT)).executable(foundOptions.has(EXECUTABLE_OPT)).properties(properties).hollow(foundOptions.has(HOLLOW_OPT));
    if (foundOptions.has(MAIN_OPT)) {
        tool.mainClass(foundOptions.valueOf(MAIN_OPT));
    }
    if (foundOptions.has(MODULES_OPT)) {
        tool.additionalModules(foundOptions.valuesOf(MODULES_OPT));
    }
    if (foundOptions.has(DEBUG_LOGGING)) {
        tool.logger(BuildTool.STD_LOGGER_WITH_DEBUG);
    }
    addSwarmFractions(tool, foundOptions.valuesOf(FRACTIONS_OPT));
    System.err.println(String.format("Building %s/%s%s.jar", outDir, jarName, suffix));
    return tool.build(jarName, Paths.get(outDir));
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DeclaredDependencies(org.wildfly.swarm.tools.DeclaredDependencies) BuildTool(org.wildfly.swarm.tools.BuildTool) OptionException(joptsimple.OptionException) OptionSet(joptsimple.OptionSet) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 7 with DeclaredDependencies

use of org.wildfly.swarm.tools.DeclaredDependencies 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)

Example 8 with DeclaredDependencies

use of org.wildfly.swarm.tools.DeclaredDependencies in project wildfly-swarm by wildfly-swarm.

the class GradleDependencyDeclarationFactory method create.

@Override
public DeclaredDependencies create(FileSystemLayout fsLayout, ShrinkwrapArtifactResolvingHelper resolvingHelper) {
    GradleDependencyAdapter gradleAdapter = new GradleDependencyAdapter(fsLayout.getRootPath());
    DeclaredDependencies declaredDependencies = gradleAdapter.parseDependencies(GradleDependencyAdapter.Configuration.TEST_RUNTIME);
    // resolve to local files
    resolvingHelper.resolveAll(new HashSet<>(declaredDependencies.getTransientDependencies()), false, false);
    return declaredDependencies;
}
Also used : DeclaredDependencies(org.wildfly.swarm.tools.DeclaredDependencies)

Aggregations

DeclaredDependencies (org.wildfly.swarm.tools.DeclaredDependencies)8 File (java.io.File)5 ArtifactSpec (org.wildfly.swarm.tools.ArtifactSpec)4 BuildTool (org.wildfly.swarm.tools.BuildTool)4 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 Artifact (org.apache.maven.artifact.Artifact)2 SimpleLogger (org.wildfly.swarm.spi.meta.SimpleLogger)2 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Method (java.lang.reflect.Method)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 Map (java.util.Map)1 Optional (java.util.Optional)1