Search in sources :

Example 1 with ScriptBlockBuilder

use of org.gradle.buildinit.plugins.internal.ScriptBlockBuilder in project gradle by gradle.

the class Maven2Gradle method globalExclusions.

private void globalExclusions(MavenProject project, BuildScriptBuilder builder) {
    Plugin enforcerPlugin = plugin("maven-enforcer-plugin", project);
    PluginExecution enforceGoal = pluginGoal("enforce", enforcerPlugin);
    if (enforceGoal != null) {
        Xpp3Dom configuration = (Xpp3Dom) enforceGoal.getConfiguration();
        Xpp3Dom bannedDependencies = configuration.getChild("rules").getChild("bannedDependencies");
        if (bannedDependencies != null) {
            Xpp3Dom[] children = bannedDependencies.getChild("excludes").getChildren();
            ScriptBlockBuilder block = builder.block(null, "configurations.all");
            if (children != null) {
                for (Xpp3Dom exclude : children) {
                    String[] tokens = exclude.getValue().split(":");
                    Map<String, String> params = new LinkedHashMap<>();
                    params.put("group", tokens[0]);
                    if (tokens.length > 1 && !tokens[1].equals("*")) {
                        params.put("module", tokens[1]);
                    }
                    block.methodInvocation(null, "exclude", params);
                }
            }
        }
    }
}
Also used : ScriptBlockBuilder(org.gradle.buildinit.plugins.internal.ScriptBlockBuilder) PluginExecution(org.apache.maven.model.PluginExecution) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Plugin(org.apache.maven.model.Plugin) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ScriptBlockBuilder

use of org.gradle.buildinit.plugins.internal.ScriptBlockBuilder in project gradle by gradle.

the class Maven2Gradle method configurePublishing.

private void configurePublishing(BuildScriptBuilder builder, boolean publishesSources, boolean testsJarTaskGenerated, boolean publishesJavadoc) {
    if (publishesSources || publishesJavadoc) {
        ScriptBlockBuilder javaExtension = builder.block(null, "java");
        if (publishesSources) {
            javaExtension.methodInvocation(null, "withSourcesJar");
        }
        if (publishesJavadoc) {
            javaExtension.methodInvocation(null, "withJavadocJar");
        }
    }
    ScriptBlockBuilder publishing = builder.block(null, "publishing");
    publishing.containerElement(null, "publications", "maven", "MavenPublication", p -> {
        p.methodInvocation(null, "from", builder.containerElementExpression("components", "java"));
        if (testsJarTaskGenerated) {
            p.methodInvocation(null, "artifact", builder.propertyExpression("testsJar"));
        }
    });
}
Also used : ScriptBlockBuilder(org.gradle.buildinit.plugins.internal.ScriptBlockBuilder)

Example 3 with ScriptBlockBuilder

use of org.gradle.buildinit.plugins.internal.ScriptBlockBuilder in project gradle by gradle.

the class Maven2Gradle method convert.

public void convert() {
    boolean multimodule = !rootProject.getModules().isEmpty();
    if (multimodule) {
        String groupId = rootProject.getGroupId();
        BuildScriptBuilder buildSrcScriptBuilder = scriptBuilderFactory.scriptForMavenConversion(dsl, "buildSrc/build", useIncubatingAPIs, insecureProtocolOption);
        buildSrcScriptBuilder.conventionPluginSupport("Support convention plugins written in " + dsl.toString() + ". Convention plugins are build scripts in 'src/main' that automatically become available as plugins in the main build.");
        buildSrcScriptBuilder.create(workingDir).generate();
        BuildScriptBuilder conventionPluginBuilder = scriptBuilderFactory.scriptForMavenConversion(dsl, "buildSrc/src/main/" + dsl.name().toLowerCase() + "/" + groupId + ".java-conventions", useIncubatingAPIs, insecureProtocolOption);
        generateSettings(rootProject.getArtifactId(), allProjects);
        Map<String, List<Dependency>> dependencies = new LinkedHashMap<>();
        for (MavenProject project : allProjects) {
            dependencies.put(project.getArtifactId(), getDependencies(project, allProjects));
        }
        coordinatesForProject(rootProject, conventionPluginBuilder);
        conventionPluginBuilder.plugin(null, "java-library");
        conventionPluginBuilder.plugin(null, "maven-publish");
        compilerSettings(rootProject, conventionPluginBuilder);
        repositoriesForProjects(allProjects, conventionPluginBuilder);
        globalExclusions(rootProject, conventionPluginBuilder);
        List<Dependency> commonDeps = dependencies.get(rootProject.getArtifactId());
        declareDependencies(commonDeps, conventionPluginBuilder);
        testNg(commonDeps, conventionPluginBuilder);
        configurePublishing(conventionPluginBuilder, packagesSources(rootProject), false, false);
        conventionPluginBuilder.create(workingDir).generate();
        for (MavenProject module : modules(allProjects, false)) {
            String id = module.getArtifactId();
            List<Dependency> moduleDependencies = dependencies.get(id);
            boolean warPack = module.getPackaging().equals("war");
            BuildScriptBuilder moduleScriptBuilder = scriptBuilderFactory.scriptForMavenConversion(dsl, RelativePathUtil.relativePath(workingDir.getAsFile(), projectDir(module)) + "/build", useIncubatingAPIs, insecureProtocolOption);
            moduleScriptBuilder.plugin(null, groupId + ".java-conventions");
            if (!module.getGroupId().equals(rootProject.getGroupId())) {
                moduleScriptBuilder.propertyAssignment(null, "group", module.getGroupId());
            }
            if (warPack) {
                moduleScriptBuilder.plugin(null, "war");
                if (dependentWars.stream().anyMatch(project -> project.getGroupId().equals(module.getGroupId()) && project.getArtifactId().equals(id))) {
                    moduleScriptBuilder.taskPropertyAssignment(null, "jar", "Jar", "enabled", true);
                }
            }
            descriptionForProject(module, moduleScriptBuilder);
            declareDependencies(moduleDependencies, moduleScriptBuilder);
            testNg(moduleDependencies, moduleScriptBuilder);
            if (packageTests(module, moduleScriptBuilder)) {
                if (dsl == BuildInitDsl.GROOVY) {
                    moduleScriptBuilder.methodInvocation(null, "publishing.publications.maven.artifact", moduleScriptBuilder.propertyExpression("testsJar"));
                } else {
                    moduleScriptBuilder.methodInvocation(null, "(publishing.publications[\"maven\"] as MavenPublication).artifact", moduleScriptBuilder.propertyExpression("testsJar"));
                }
            }
            if (packagesJavadocs(module)) {
                ScriptBlockBuilder javaExtension = moduleScriptBuilder.block(null, "java");
                javaExtension.methodInvocation(null, "withJavadocJar");
            }
            moduleScriptBuilder.create(workingDir).generate();
        }
    } else {
        BuildScriptBuilder scriptBuilder = scriptBuilderFactory.scriptForMavenConversion(dsl, "build", useIncubatingAPIs, insecureProtocolOption);
        generateSettings(this.rootProject.getArtifactId(), Collections.emptySet());
        scriptBuilder.plugin(null, "java");
        scriptBuilder.plugin(null, "maven-publish");
        coordinatesForProject(this.rootProject, scriptBuilder);
        descriptionForProject(this.rootProject, scriptBuilder);
        compilerSettings(this.rootProject, scriptBuilder);
        globalExclusions(this.rootProject, scriptBuilder);
        boolean testsJarTaskGenerated = packageTests(this.rootProject, scriptBuilder);
        configurePublishing(scriptBuilder, packagesSources(this.rootProject), testsJarTaskGenerated, packagesJavadocs(this.rootProject));
        scriptBuilder.repositories().mavenLocal(null);
        Set<String> repoSet = new LinkedHashSet<>();
        getRepositoriesForModule(this.rootProject, repoSet);
        for (String repo : repoSet) {
            scriptBuilder.repositories().maven(null, repo);
        }
        List<Dependency> dependencies = getDependencies(this.rootProject, null);
        declareDependencies(dependencies, scriptBuilder);
        testNg(dependencies, scriptBuilder);
        scriptBuilder.create(workingDir).generate();
    }
}
Also used : ScriptBlockBuilder(org.gradle.buildinit.plugins.internal.ScriptBlockBuilder) LinkedHashSet(java.util.LinkedHashSet) BuildScriptBuilder(org.gradle.buildinit.plugins.internal.BuildScriptBuilder) MavenProject(org.apache.maven.project.MavenProject) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ScriptBlockBuilder (org.gradle.buildinit.plugins.internal.ScriptBlockBuilder)3 LinkedHashMap (java.util.LinkedHashMap)2 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Plugin (org.apache.maven.model.Plugin)1 PluginExecution (org.apache.maven.model.PluginExecution)1 MavenProject (org.apache.maven.project.MavenProject)1 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)1 BuildScriptBuilder (org.gradle.buildinit.plugins.internal.BuildScriptBuilder)1