Search in sources :

Example 1 with BuildScriptBuilder

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

the class Maven2Gradle method generateSettings.

private void generateSettings(String mvnProjectName, Set<MavenProject> projects) {
    BuildScriptBuilder scriptBuilder = scriptBuilderFactory.scriptForMavenConversion(dsl, "settings", useIncubatingAPIs, insecureProtocolOption);
    scriptBuilder.propertyAssignment(null, "rootProject.name", mvnProjectName);
    Set<MavenProject> modules = modules(projects, true);
    List<String> moduleNames = new ArrayList<>();
    Map<String, String> artifactIdToDir = new LinkedHashMap<>();
    for (MavenProject project : modules) {
        String fqn = fqn(project, projects);
        File projectDirectory = projectDir(project);
        // don't add project if it's the rootproject
        if (!workingDir.getAsFile().equals(projectDirectory)) {
            moduleNames.add(fqn);
            // Calculate the path to the project, ignore this path if it's the default value
            String relativePath = RelativePathUtil.relativePath(workingDir.getAsFile(), projectDirectory);
            if (!fqn.equals(":" + relativePath)) {
                artifactIdToDir.put(fqn, relativePath);
            }
        }
    }
    for (String name : moduleNames) {
        scriptBuilder.methodInvocation(null, "include", name);
    }
    for (Map.Entry<String, String> entry : artifactIdToDir.entrySet()) {
        BuildScriptBuilder.Expression dirExpression = scriptBuilder.methodInvocationExpression("file", entry.getValue());
        scriptBuilder.propertyAssignment(null, "project(\"" + entry.getKey() + "\").projectDir", dirExpression);
    }
    scriptBuilder.create(workingDir).generate();
}
Also used : BuildScriptBuilder(org.gradle.buildinit.plugins.internal.BuildScriptBuilder) MavenProject(org.apache.maven.project.MavenProject) ArrayList(java.util.ArrayList) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with BuildScriptBuilder

use of org.gradle.buildinit.plugins.internal.BuildScriptBuilder 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

ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 MavenProject (org.apache.maven.project.MavenProject)2 BuildScriptBuilder (org.gradle.buildinit.plugins.internal.BuildScriptBuilder)2 File (java.io.File)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 ScriptBlockBuilder (org.gradle.buildinit.plugins.internal.ScriptBlockBuilder)1