Search in sources :

Example 1 with Jar

use of org.gradle.api.tasks.bundling.Jar in project intellij-community by JetBrains.

the class ModuleExtendedModelBuilderImpl method buildAll.

@Nullable
@Override
public Object buildAll(String modelName, Project project) {
    final String moduleName = project.getName();
    final String moduleGroup = project.getGroup().toString();
    final String moduleVersion = project.getVersion().toString();
    final File buildDir = project.getBuildDir();
    String javaSourceCompatibility = null;
    for (Task task : project.getTasks()) {
        if (task instanceof JavaCompile) {
            JavaCompile javaCompile = (JavaCompile) task;
            javaSourceCompatibility = javaCompile.getSourceCompatibility();
            if (task.getName().equals("compileJava"))
                break;
        }
    }
    final ModuleExtendedModelImpl moduleVersionModel = new ModuleExtendedModelImpl(moduleName, moduleGroup, moduleVersion, buildDir, javaSourceCompatibility);
    final List<File> artifacts = new ArrayList<File>();
    for (Task task : project.getTasks()) {
        if (task instanceof Jar) {
            Jar jar = (Jar) task;
            artifacts.add(jar.getArchivePath());
        }
    }
    moduleVersionModel.setArtifacts(artifacts);
    final Set<String> sourceDirectories = new HashSet<String>();
    final Set<String> testDirectories = new HashSet<String>();
    final Set<String> resourceDirectories = new HashSet<String>();
    final Set<String> testResourceDirectories = new HashSet<String>();
    final List<File> testClassesDirs = new ArrayList<File>();
    for (Task task : project.getTasks()) {
        if (task instanceof Test) {
            Test test = (Test) task;
            testClassesDirs.add(test.getTestClassesDir());
            if (test.hasProperty(TEST_SRC_DIRS_PROPERTY)) {
                Object testSrcDirs = test.property(TEST_SRC_DIRS_PROPERTY);
                if (testSrcDirs instanceof Iterable) {
                    for (Object dir : Iterable.class.cast(testSrcDirs)) {
                        addFilePath(testDirectories, dir);
                    }
                }
            }
        }
    }
    IdeaCompilerOutputImpl compilerOutput = new IdeaCompilerOutputImpl();
    if (project.hasProperty(SOURCE_SETS_PROPERTY)) {
        Object sourceSets = project.property(SOURCE_SETS_PROPERTY);
        if (sourceSets instanceof SourceSetContainer) {
            SourceSetContainer sourceSetContainer = (SourceSetContainer) sourceSets;
            for (SourceSet sourceSet : sourceSetContainer) {
                SourceSetOutput output = sourceSet.getOutput();
                if (SourceSet.TEST_SOURCE_SET_NAME.equals(sourceSet.getName())) {
                    compilerOutput.setTestClassesDir(output.getClassesDir());
                    compilerOutput.setTestResourcesDir(output.getResourcesDir());
                }
                if (SourceSet.MAIN_SOURCE_SET_NAME.equals(sourceSet.getName())) {
                    compilerOutput.setMainClassesDir(output.getClassesDir());
                    compilerOutput.setMainResourcesDir(output.getResourcesDir());
                }
                for (File javaSrcDir : sourceSet.getAllJava().getSrcDirs()) {
                    boolean isTestDir = isTestDir(sourceSet, testClassesDirs);
                    addFilePath(isTestDir ? testDirectories : sourceDirectories, javaSrcDir);
                }
                for (File resourcesSrcDir : sourceSet.getResources().getSrcDirs()) {
                    boolean isTestDir = isTestDir(sourceSet, testClassesDirs);
                    addFilePath(isTestDir ? testResourceDirectories : resourceDirectories, resourcesSrcDir);
                }
            }
        }
    }
    File projectDir = project.getProjectDir();
    IdeaContentRootImpl contentRoot = new IdeaContentRootImpl(projectDir);
    final Set<String> ideaSourceDirectories = new HashSet<String>();
    final Set<String> ideaTestDirectories = new HashSet<String>();
    final Set<String> ideaGeneratedDirectories = new HashSet<String>();
    final Set<File> excludeDirectories = new HashSet<File>();
    enrichDataFromIdeaPlugin(project, excludeDirectories, ideaSourceDirectories, ideaTestDirectories, ideaGeneratedDirectories);
    if (ideaSourceDirectories.isEmpty()) {
        sourceDirectories.clear();
        resourceDirectories.clear();
    }
    if (ideaTestDirectories.isEmpty()) {
        testDirectories.clear();
        testResourceDirectories.clear();
    }
    ideaSourceDirectories.removeAll(resourceDirectories);
    sourceDirectories.removeAll(ideaTestDirectories);
    sourceDirectories.addAll(ideaSourceDirectories);
    ideaTestDirectories.removeAll(testResourceDirectories);
    testDirectories.addAll(ideaTestDirectories);
    // ensure disjoint directories with different type
    resourceDirectories.removeAll(sourceDirectories);
    testDirectories.removeAll(sourceDirectories);
    testResourceDirectories.removeAll(testDirectories);
    for (String javaDir : sourceDirectories) {
        contentRoot.addSourceDirectory(new IdeaSourceDirectoryImpl(new File(javaDir), ideaGeneratedDirectories.contains(javaDir)));
    }
    for (String testDir : testDirectories) {
        contentRoot.addTestDirectory(new IdeaSourceDirectoryImpl(new File(testDir), ideaGeneratedDirectories.contains(testDir)));
    }
    for (String resourceDir : resourceDirectories) {
        contentRoot.addResourceDirectory(new IdeaSourceDirectoryImpl(new File(resourceDir)));
    }
    for (String testResourceDir : testResourceDirectories) {
        contentRoot.addTestResourceDirectory(new IdeaSourceDirectoryImpl(new File(testResourceDir)));
    }
    for (File excludeDir : excludeDirectories) {
        contentRoot.addExcludeDirectory(excludeDir);
    }
    moduleVersionModel.setContentRoots(Collections.<ExtIdeaContentRoot>singleton(contentRoot));
    moduleVersionModel.setCompilerOutput(compilerOutput);
    ConfigurationContainer configurations = project.getConfigurations();
    SortedMap<String, Configuration> configurationsByName = configurations.getAsMap();
    Map<String, Set<File>> artifactsByConfiguration = new HashMap<String, Set<File>>();
    for (Map.Entry<String, Configuration> configurationEntry : configurationsByName.entrySet()) {
        Set<File> files = configurationEntry.getValue().getAllArtifacts().getFiles().getFiles();
        artifactsByConfiguration.put(configurationEntry.getKey(), files);
    }
    moduleVersionModel.setArtifactsByConfiguration(artifactsByConfiguration);
    return moduleVersionModel;
}
Also used : Task(org.gradle.api.Task) SourceSet(org.gradle.api.tasks.SourceSet) Configuration(org.gradle.api.artifacts.Configuration) IdeaContentRootImpl(org.jetbrains.plugins.gradle.tooling.internal.IdeaContentRootImpl) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) SourceSetOutput(org.gradle.api.tasks.SourceSetOutput) ModuleExtendedModelImpl(org.jetbrains.plugins.gradle.tooling.internal.ModuleExtendedModelImpl) SourceSet(org.gradle.api.tasks.SourceSet) Test(org.gradle.api.tasks.testing.Test) IdeaSourceDirectoryImpl(org.jetbrains.plugins.gradle.tooling.internal.IdeaSourceDirectoryImpl) IdeaCompilerOutputImpl(org.jetbrains.plugins.gradle.tooling.internal.IdeaCompilerOutputImpl) ConfigurationContainer(org.gradle.api.artifacts.ConfigurationContainer) Jar(org.gradle.api.tasks.bundling.Jar) File(java.io.File) JavaCompile(org.gradle.api.tasks.compile.JavaCompile) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with Jar

use of org.gradle.api.tasks.bundling.Jar in project gradle by gradle.

the class JavaLibraryDistributionPlugin method apply.

@Override
public void apply(final ProjectInternal project) {
    this.project = project;
    project.getPluginManager().apply(JavaPlugin.class);
    project.getPluginManager().apply(DistributionPlugin.class);
    DefaultDistributionContainer defaultDistributionContainer = (DefaultDistributionContainer) project.getExtensions().findByName("distributions");
    CopySpec contentSpec = defaultDistributionContainer.getByName(DistributionPlugin.MAIN_DISTRIBUTION_NAME).getContents();
    Jar jar = (Jar) project.getTasks().getByName(JavaPlugin.JAR_TASK_NAME);
    CopySpec childSpec = project.copySpec();
    childSpec.from(jar);
    childSpec.from(project.file("src/dist"));
    CopySpec libSpec = project.copySpec();
    libSpec.into("lib");
    libSpec.from(project.getConfigurations().getByName("runtime"));
    childSpec.with(libSpec);
    contentSpec.with(childSpec);
}
Also used : CopySpec(org.gradle.api.file.CopySpec) Jar(org.gradle.api.tasks.bundling.Jar) DefaultDistributionContainer(org.gradle.api.distribution.internal.DefaultDistributionContainer)

Example 3 with Jar

use of org.gradle.api.tasks.bundling.Jar in project gradle by gradle.

the class JavaPlugin method configureArchivesAndComponent.

private void configureArchivesAndComponent(Project project, JavaPluginConvention pluginConvention) {
    Jar jar = project.getTasks().create(JAR_TASK_NAME, Jar.class);
    jar.setDescription("Assembles a jar archive containing the main classes.");
    jar.setGroup(BasePlugin.BUILD_GROUP);
    jar.from(pluginConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME).getOutput());
    ArchivePublishArtifact jarArtifact = new ArchivePublishArtifact(jar);
    Configuration apiElementConfiguration = project.getConfigurations().getByName(API_ELEMENTS_CONFIGURATION_NAME);
    Configuration runtimeConfiguration = project.getConfigurations().getByName(RUNTIME_CONFIGURATION_NAME);
    Configuration runtimeElementsConfiguration = project.getConfigurations().getByName(RUNTIME_ELEMENTS_CONFIGURATION_NAME);
    project.getExtensions().getByType(DefaultArtifactPublicationSet.class).addCandidate(jarArtifact);
    JavaCompile javaCompile = (JavaCompile) project.getTasks().getByPath(COMPILE_JAVA_TASK_NAME);
    ProcessResources processResources = (ProcessResources) project.getTasks().getByPath(PROCESS_RESOURCES_TASK_NAME);
    addJar(apiElementConfiguration, jarArtifact);
    addJar(runtimeConfiguration, jarArtifact);
    addRuntimeVariants(runtimeElementsConfiguration, jarArtifact, javaCompile, processResources);
    project.getComponents().add(objectFactory.newInstance(JavaLibrary.class, project.getConfigurations(), jarArtifact));
    project.getComponents().add(objectFactory.newInstance(JavaLibraryPlatform.class, project.getConfigurations()));
}
Also used : DefaultArtifactPublicationSet(org.gradle.api.internal.plugins.DefaultArtifactPublicationSet) ArchivePublishArtifact(org.gradle.api.internal.artifacts.publish.ArchivePublishArtifact) Configuration(org.gradle.api.artifacts.Configuration) JavaLibrary(org.gradle.api.internal.java.JavaLibrary) JavaLibraryPlatform(org.gradle.api.internal.java.JavaLibraryPlatform) ProcessResources(org.gradle.language.jvm.tasks.ProcessResources) Jar(org.gradle.api.tasks.bundling.Jar) JavaCompile(org.gradle.api.tasks.compile.JavaCompile)

Example 4 with Jar

use of org.gradle.api.tasks.bundling.Jar in project gradle by gradle.

the class OsgiPlugin method apply.

public void apply(final Project project) {
    project.getPluginManager().apply(JavaBasePlugin.class);
    final OsgiPluginConvention osgiConvention = new OsgiPluginConvention((ProjectInternal) project);
    project.getConvention().getPlugins().put("osgi", osgiConvention);
    project.getPlugins().withType(JavaPlugin.class, new Action<JavaPlugin>() {

        @Override
        public void execute(JavaPlugin javaPlugin) {
            // When creating the OSGi manifest, we must have a single view of all of the classes included in the jar.
            Sync prepareOsgiClasses = project.getTasks().create("osgiClasses", Sync.class);
            FileCollection classes = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().getByName("main").getOutput().getClassesDirs();
            File singleClassesDirectory = new File(project.getBuildDir(), "osgi-classes");
            prepareOsgiClasses.setDescription("Prepares a single classes directory required for OSGi analysis.");
            prepareOsgiClasses.from(classes);
            prepareOsgiClasses.into(singleClassesDirectory);
            Jar jarTask = (Jar) project.getTasks().getByName("jar");
            jarTask.dependsOn(prepareOsgiClasses);
            OsgiManifest osgiManifest = osgiConvention.osgiManifest();
            osgiManifest.setClassesDir(singleClassesDirectory);
            osgiManifest.setClasspath(project.getConfigurations().getByName("runtime"));
            jarTask.setManifest(osgiManifest);
        }
    });
}
Also used : JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) JavaPlugin(org.gradle.api.plugins.JavaPlugin) Jar(org.gradle.api.tasks.bundling.Jar) Sync(org.gradle.api.tasks.Sync) FileCollection(org.gradle.api.file.FileCollection) File(java.io.File)

Example 5 with Jar

use of org.gradle.api.tasks.bundling.Jar in project shipkit by mockito.

the class JavaLibraryPlugin method apply.

@Override
public void apply(final Project project) {
    project.getPlugins().apply("java");
    final CopySpec license = project.copySpec(new Action<CopySpec>() {

        public void execute(CopySpec copy) {
            copy.from(project.getRootDir()).include("LICENSE");
        }
    });
    ((Jar) project.getTasks().getByName("jar")).with(license);
    final Jar sourcesJar = project.getTasks().create(SOURCES_JAR_TASK, Jar.class, new Action<Jar>() {

        public void execute(Jar jar) {
            jar.from(JavaPluginUtil.getMainSourceSet(project).getAllSource());
            jar.setClassifier("sources");
            jar.with(license);
        }
    });
    final Task javadocJar = project.getTasks().create(JAVADOC_JAR_TASK, Jar.class, new Action<Jar>() {

        public void execute(Jar jar) {
            jar.from(project.getTasks().getByName("javadoc"));
            jar.setClassifier("javadoc");
            jar.with(license);
        }
    });
    project.getArtifacts().add("archives", sourcesJar);
    project.getArtifacts().add("archives", javadocJar);
}
Also used : CopySpec(org.gradle.api.file.CopySpec) Jar(org.gradle.api.tasks.bundling.Jar)

Aggregations

Jar (org.gradle.api.tasks.bundling.Jar)22 Configuration (org.gradle.api.artifacts.Configuration)12 File (java.io.File)10 Task (org.gradle.api.Task)10 JavaPlugin (org.gradle.api.plugins.JavaPlugin)10 JavaCompile (org.gradle.api.tasks.compile.JavaCompile)10 Project (org.gradle.api.Project)9 SourceSet (org.gradle.api.tasks.SourceSet)9 SourceSetContainer (org.gradle.api.tasks.SourceSetContainer)9 Collections (java.util.Collections)8 List (java.util.List)8 Set (java.util.Set)8 Plugin (org.gradle.api.Plugin)8 FileCollection (org.gradle.api.file.FileCollection)8 JavaPluginExtension (org.gradle.api.plugins.JavaPluginExtension)8 IdlOptions (com.linkedin.pegasus.gradle.PegasusOptions.IdlOptions)6 CompatibilityLogChecker (com.linkedin.pegasus.gradle.internal.CompatibilityLogChecker)6 ChangedFileReportTask (com.linkedin.pegasus.gradle.tasks.ChangedFileReportTask)6 CheckIdlTask (com.linkedin.pegasus.gradle.tasks.CheckIdlTask)6 CheckPegasusSnapshotTask (com.linkedin.pegasus.gradle.tasks.CheckPegasusSnapshotTask)6