use of org.gradle.api.tasks.compile.JavaCompile 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;
}
use of org.gradle.api.tasks.compile.JavaCompile in project gradle by gradle.
the class JavaLibraryPlugin method addApiToMainSourceSet.
private void addApiToMainSourceSet(Project project, JavaPluginConvention convention, ConfigurationContainer configurations) {
SourceSet sourceSet = convention.getSourceSets().getByName("main");
Configuration apiConfiguration = configurations.maybeCreate(sourceSet.getApiConfigurationName());
apiConfiguration.setVisible(false);
apiConfiguration.setDescription("API dependencies for " + sourceSet + ".");
apiConfiguration.setCanBeResolved(false);
apiConfiguration.setCanBeConsumed(false);
Configuration apiElementsConfiguration = configurations.getByName(sourceSet.getApiElementsConfigurationName());
apiElementsConfiguration.extendsFrom(apiConfiguration);
final JavaCompile javaCompile = (JavaCompile) project.getTasks().getByPath(COMPILE_JAVA_TASK_NAME);
// Define a classes variant to use for compilation
ConfigurationPublications publications = apiElementsConfiguration.getOutgoing();
ConfigurationVariant variant = publications.getVariants().create("classes");
variant.getAttributes().attribute(Usage.USAGE_ATTRIBUTE, objectFactory.named(Usage.class, Usage.JAVA_API_CLASSES));
variant.artifact(new JavaPlugin.IntermediateJavaArtifact(ArtifactTypeDefinition.JVM_CLASS_DIRECTORY, javaCompile) {
@Override
public File getFile() {
return javaCompile.getDestinationDir();
}
});
Configuration implementationConfiguration = configurations.getByName(sourceSet.getImplementationConfigurationName());
implementationConfiguration.extendsFrom(apiConfiguration);
Configuration compileConfiguration = configurations.getByName(sourceSet.getCompileConfigurationName());
apiConfiguration.extendsFrom(compileConfiguration);
}
use of org.gradle.api.tasks.compile.JavaCompile 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()));
}
use of org.gradle.api.tasks.compile.JavaCompile in project core-java by SpineEventEngine.
the class ModelVerifierShould method retrieve_compilation_dest_dir_from_task.
@Test
public void retrieve_compilation_dest_dir_from_task() throws MalformedURLException {
final JavaCompile compileTask = actualProject().getTasks().withType(JavaCompile.class).getByName(COMPILE_JAVA.getValue());
final File dest = Files.createTempDir();
compileTask.setDestinationDir(dest);
final Function<JavaCompile, URL> func = ModelVerifier.GetDestinationDir.FUNCTION;
final URL destUrl = dest.toURI().toURL();
assertEquals(destUrl, func.apply(compileTask));
}
use of org.gradle.api.tasks.compile.JavaCompile in project core-java by SpineEventEngine.
the class ModelVerifierShould method retrieve_null_if_dest_dir_is_null.
@Test
public void retrieve_null_if_dest_dir_is_null() {
final JavaCompile compileTask = mock(JavaCompile.class);
final Function<JavaCompile, URL> func = ModelVerifier.GetDestinationDir.FUNCTION;
assertNull(func.apply(compileTask));
}
Aggregations