private List<String> buildCompilerArgs(JavaPluginConvention javaPluginConvention) { TaskCollection<JavaCompile> javaCompilers = getProject().getTasks().withType(JavaCompile.class); CompileOptions firstCompilerOptions = javaCompilers.isEmpty() ? null : javaCompilers.iterator().next().getOptions(); List<String> args = new ArrayList<>(Arrays.asList("-source", javaPluginConvention.getSourceCompatibility().toString(), "-target", javaPluginConvention.getTargetCompatibility().toString(), "-encoding", getDefaultOrCompilerEncoding(firstCompilerOptions))); if (firstCompilerOptions != null) { FileCollection bootClasspath = firstCompilerOptions.getBootstrapClasspath(); if (bootClasspath != null) { args.add("-bootclasspath"); args.add(bootClasspath.getAsPath()); } } return args; }
private JavaCompiler.CompilationTask createCompileTask(JavaCompileSpec spec) { List<String> options = new JavaCompilerArgumentsBuilder(spec).build(); JavaCompiler compiler = javaHomeBasedJavaCompilerFactory.create(); CompileOptions compileOptions = spec.getCompileOptions(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, compileOptions.getEncoding() != null ? Charset.forName(compileOptions.getEncoding()) : null); Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(spec.getSource()); return compiler.getTask(null, null, null, options, null, compilationUnits); }
public Compiler<GroovyJavaJointCompileSpec> newCompiler(GroovyJavaJointCompileSpec spec) { CompileOptions javaOptions = spec.getCompileOptions(); GroovyCompileOptions groovyOptions = spec.getGroovyCompileOptions(); Compiler<JavaCompileSpec> javaCompiler = javaCompilerFactory.createForJointCompilation(javaOptions); Compiler<GroovyJavaJointCompileSpec> groovyCompiler = new ApiGroovyCompiler(javaCompiler); CompilerDaemonFactory daemonFactory; if (groovyOptions.isFork()) { daemonFactory = compilerDaemonFactory; } else { daemonFactory = inProcessCompilerDaemonFactory; } groovyCompiler = new DaemonGroovyCompiler(project.getRootProject().getProjectDir(), groovyCompiler, project.getServices().get(ClassPathRegistry.class), daemonFactory); return new NormalizingGroovyCompiler(groovyCompiler); }
private Compiler<JavaCompileSpec> createTargetCompiler(CompileOptions options, boolean jointCompilation) { if (options.isFork() && options.getForkOptions().getExecutable() != null) { return new CommandLineJavaCompiler(); } Compiler<JavaCompileSpec> compiler = new JdkJavaCompiler(); if (options.isFork() && !jointCompilation) { return new DaemonJavaCompiler(daemonWorkingDir, compiler, compilerDaemonFactory); } return compiler; }
private JavaCompiler.CompilationTask createCompileTask(JavaCompileSpec spec) { List<String> options = new JavaCompilerArgumentsBuilder(spec).build(); JavaCompiler compiler = findCompiler(); if(compiler==null){ throw new RuntimeException("Cannot find System Java Compiler. Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable to point to the according directory."); } CompileOptions compileOptions = spec.getCompileOptions(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, compileOptions.getEncoding() != null ? Charset.forName(compileOptions.getEncoding()) : null); Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(spec.getSource()); return compiler.getTask(null, null, null, options, null, compilationUnits); }
public Compiler<JavaCompileSpec> create(CompileOptions options) { if (JavaVersion.current().isJava6Compatible()) { return createJdk6Compiler(); } if (SUN_COMPILER_AVAILABLE) { return new SunCompilerFactory().create(); } throw new RuntimeException("Cannot find a Java compiler API. Please let us know which JDK/platform you are using. To work around this problem, try 'compileJava.options.useAnt=true'."); }
public void compilationComplete(CompileOptions options, ClassDependencyInfoExtractor extractor, ClassDependencyInfoSerializer serializer, Iterable<JarArchive> jarsOnClasspath) { if (options.isIncremental()) { Clock clock = new Clock(); ClassDependencyInfo info = extractor.extractInfo(""); serializer.writeInfo(info); LOG.lifecycle("Performed class dependency analysis in {}, wrote results into {}", clock.getTime(), serializer); // clock = new Clock(); // jarSnapshotFeeder.storeJarSnapshots(jarsOnClasspath); // LOG.lifecycle("Wrote jar snapshots in {}.", clock.getTime()); } }
Compiler<GroovyJavaJointCompileSpec> create(final GroovyCompileOptions groovyOptions, final CompileOptions javaOptions) { return DeprecationLogger.whileDisabled(new Factory<Compiler<GroovyJavaJointCompileSpec>>() { public Compiler<GroovyJavaJointCompileSpec> create() { // Some sanity checking of options if (groovyOptions.isUseAnt() && !javaOptions.isUseAnt()) { LOGGER.warn("When groovyOptions.useAnt is enabled, options.useAnt must also be enabled. Ignoring options.useAnt = false."); javaOptions.setUseAnt(true); } else if (!groovyOptions.isUseAnt() && javaOptions.isUseAnt()) { LOGGER.warn("When groovyOptions.useAnt is disabled, options.useAnt must also be disabled. Ignoring options.useAnt = true."); javaOptions.setUseAnt(false); } if (groovyOptions.isUseAnt()) { return new AntGroovyCompiler(antBuilder, classPathRegistry); } javaCompilerFactory.setJointCompilation(true); Compiler<JavaCompileSpec> javaCompiler = javaCompilerFactory.create(javaOptions); Compiler<GroovyJavaJointCompileSpec> groovyCompiler = new ApiGroovyCompiler(javaCompiler); CompilerDaemonFactory daemonFactory; if (groovyOptions.isFork()) { daemonFactory = compilerDaemonManager; } else { daemonFactory = inProcessCompilerDaemonFactory; } groovyCompiler = new DaemonGroovyCompiler(project, groovyCompiler, daemonFactory); return new NormalizingGroovyCompiler(groovyCompiler); } }); }
public Compiler<JavaCompileSpec> create(CompileOptions options) { fallBackToAntIfNecessary(options); if (options.isUseAnt()) { return new AntJavaCompiler(antBuilderFactory); } Compiler<JavaCompileSpec> result = createTargetCompiler(options); if (!jointCompilation) { result = new NormalizingJavaCompiler(result); } return result; }
private void fallBackToAntIfNecessary(CompileOptions options) { if (options.isUseAnt()) { return; } if (options.getCompiler() != null) { LOGGER.warn("Falling back to Ant javac task ('CompileOptions.useAnt = true') because 'CompileOptions.compiler' is set."); options.setUseAnt(true); } }
private Compiler<JavaCompileSpec> createTargetCompiler(CompileOptions options) { if (options.isFork() && options.getForkOptions().getExecutable() != null) { return new CommandLineJavaCompiler(createSerializableTempFileProvider(), project.getProjectDir()); } Compiler<JavaCompileSpec> compiler = inProcessCompilerFactory.create(options); if (options.isFork() && !jointCompilation) { return new DaemonJavaCompiler(project, compiler, compilerDaemonManager); } return compiler; }
public DefaultGroovyJavaJointCompileSpecFactory(CompileOptions compileOptions) { super(compileOptions); }
@Override public CompileOptions getCompileOptions() { return compileOptions; }
public void setCompileOptions(CompileOptions compileOptions) { this.compileOptions = compileOptions; }
private void addMainOptions() { if (!includeMainOptions) { return; } CompileOptions compileOptions = spec.getCompileOptions(); List<String> compilerArgs = compileOptions.getCompilerArgs(); if (!releaseOptionIsSet(compilerArgs)) { String sourceCompatibility = spec.getSourceCompatibility(); if (sourceCompatibility != null) { args.add("-source"); args.add(sourceCompatibility); } String targetCompatibility = spec.getTargetCompatibility(); if (targetCompatibility != null) { args.add("-target"); args.add(targetCompatibility); } } File destinationDir = spec.getDestinationDir(); if (destinationDir != null) { args.add("-d"); args.add(destinationDir.getPath()); } if (compileOptions.isVerbose()) { args.add("-verbose"); } if (compileOptions.isDeprecation()) { args.add("-deprecation"); } if (!compileOptions.isWarnings()) { args.add("-nowarn"); } if (compileOptions.isDebug()) { if (compileOptions.getDebugOptions().getDebugLevel() != null) { args.add("-g:" + compileOptions.getDebugOptions().getDebugLevel().trim()); } else { args.add("-g"); } } else { args.add("-g:none"); } if (compileOptions.getEncoding() != null) { args.add("-encoding"); args.add(compileOptions.getEncoding()); } if (compileOptions.getBootClasspath() != null) { //TODO: move bootclasspath to platform args.add("-bootclasspath"); args.add(compileOptions.getBootClasspath()); } if (compileOptions.getExtensionDirs() != null) { args.add("-extdirs"); args.add(compileOptions.getExtensionDirs()); } FileCollection sourcepath = compileOptions.getSourcepath(); Iterable<File> classpath = spec.getClasspath(); if ((sourcepath != null && !sourcepath.isEmpty()) || (includeClasspath && (classpath != null && classpath.iterator().hasNext()))) { args.add("-sourcepath"); args.add(sourcepath == null ? emptyFolder(spec.getTempDir()) : sourcepath.getAsPath()); } if (compilerArgs != null) { args.addAll(compilerArgs); } }
public AbstractJavaCompileSpecFactory(CompileOptions compileOptions) { this.compileOptions = compileOptions; }
public AntDependsStaleClassCleaner(Factory<AntBuilder> antBuilderFactory, CompileOptions compileOptions) { this.antBuilderFactory = antBuilderFactory; this.compileOptions = compileOptions; }
public DefaultJavaCompileSpecFactory(CompileOptions compileOptions) { super(compileOptions); }
public DefaultScalaJavaJointCompileSpecFactory(CompileOptions compileOptions) { super(compileOptions); }
/** * Returns the Java compilation options. */ @Nested public CompileOptions getOptions() { return compileOptions; }
/** * Configure the compile tasks * * @param javaCompile Compile task to configure * @param project Project context * @param javaConfig Java configuration * @param configurations Configurations containing compiler configuration */ @Mutate public void configureCompileTask(@Each JavaCompile javaCompile, ProjectContext project, JavaConfig javaConfig, ConfigurationContainer configurations) { File propertiesFile = new File(project.getBuildDir(), "tmp/" + javaCompile.getName() + "/eclipseJavaCompiler.prefs"); javaCompile.doFirst(t -> { propertiesFile.getParentFile().mkdirs(); Properties properties = Classes.loadProperties(JavaConfigPlugin.class, "eclipseJavaCompiler.prefs"); properties.putAll(javaConfig.getEclipseCompilerProperties()); try (FileWriter out = new FileWriter(propertiesFile)) { properties.store(out, "Eclipse Java Compiler options for task " + t.getName()); } catch (IOException e) { throw new GradleException(String.format("Could not write properties file '%s'", propertiesFile), e); } }); Configuration compilerConfiguration = configurations.getByName(ECLIPSE_JAVA_COMPILER_CONFIGURATION); CompileOptions options = javaCompile.getOptions(); List<String> compilerArgs = new ArrayList<>(); compilerArgs.add("-properties"); compilerArgs.add(propertiesFile.getPath()); if (Boolean.TRUE.equals(javaConfig.isOptimize())) { compilerArgs.add("-o"); } if (Boolean.TRUE.equals(javaConfig.isDebug())) { compilerArgs.add("-g:lines,vars,source"); } else { compilerArgs.add("-g:none"); } options.setCompilerArgs(compilerArgs); options.setFork(true); String[] jvmArgs = new String[] { "-classpath", compilerConfiguration.getAsPath(), "org.eclipse.jdt.internal.compiler.batch.Main" }; ForkOptions forkOptions = options.getForkOptions(); forkOptions.setExecutable("java"); forkOptions.setJvmArgs(Arrays.asList(jvmArgs)); }
public CompileOptions getCompileOptions() { return compileOptions; }
private void addMainOptions() { if (!includeMainOptions) { return; } String sourceCompatibility = spec.getSourceCompatibility(); if (sourceCompatibility != null && !JavaVersion.current().equals(JavaVersion.toVersion(sourceCompatibility))) { args.add("-source"); args.add(sourceCompatibility); } String targetCompatibility = spec.getTargetCompatibility(); if (targetCompatibility != null && !JavaVersion.current().equals(JavaVersion.toVersion(targetCompatibility))) { args.add("-target"); args.add(targetCompatibility); } File destinationDir = spec.getDestinationDir(); if (destinationDir != null) { args.add("-d"); args.add(destinationDir.getPath()); } CompileOptions compileOptions = spec.getCompileOptions(); if (compileOptions.isVerbose()) { args.add("-verbose"); } if (compileOptions.isDeprecation()) { args.add("-deprecation"); } if (!compileOptions.isWarnings()) { args.add("-nowarn"); } if (compileOptions.isDebug()) { if (compileOptions.getDebugOptions().getDebugLevel() != null) { args.add("-g:" + compileOptions.getDebugOptions().getDebugLevel().trim()); } else { args.add("-g"); } } else { args.add("-g:none"); } if (compileOptions.getEncoding() != null) { args.add("-encoding"); args.add(compileOptions.getEncoding()); } if (compileOptions.getBootClasspath() != null) { //TODO: move bootclasspath to platform args.add("-bootclasspath"); args.add(compileOptions.getBootClasspath()); } if (compileOptions.getExtensionDirs() != null) { args.add("-extdirs"); args.add(compileOptions.getExtensionDirs()); } if (compileOptions.getCompilerArgs() != null) { args.addAll(compileOptions.getCompilerArgs()); } }
public Compiler<JavaCompileSpec> createForJointCompilation(CompileOptions options) { return createTargetCompiler(options, true); }
public Compiler<JavaCompileSpec> create(CompileOptions options) { Compiler<JavaCompileSpec> result = createTargetCompiler(options, false); return new NormalizingJavaCompiler(result); }
private void addMainOptions() { if (!includeMainOptions) { return; } String sourceCompatibility = spec.getSourceCompatibility(); if (sourceCompatibility != null && !JavaVersion.current().equals(JavaVersion.toVersion(sourceCompatibility))) { args.add("-source"); args.add(sourceCompatibility); } String targetCompatibility = spec.getTargetCompatibility(); if (targetCompatibility != null && !JavaVersion.current().equals(JavaVersion.toVersion(targetCompatibility))) { args.add("-target"); args.add(targetCompatibility); } File destinationDir = spec.getDestinationDir(); if (destinationDir != null) { args.add("-d"); args.add(destinationDir.getPath()); } CompileOptions compileOptions = spec.getCompileOptions(); if (compileOptions.isVerbose()) { args.add("-verbose"); } if (compileOptions.isDeprecation()) { args.add("-deprecation"); } if (!compileOptions.isWarnings()) { args.add("-nowarn"); } if (compileOptions.isDebug()) { if (compileOptions.getDebugOptions().getDebugLevel() != null) { args.add("-g:" + compileOptions.getDebugOptions().getDebugLevel().trim()); } else { args.add("-g"); } } else { args.add("-g:none"); } if (compileOptions.getEncoding() != null) { args.add("-encoding"); args.add(compileOptions.getEncoding()); } if (compileOptions.getBootClasspath() != null) { args.add("-bootclasspath"); args.add(compileOptions.getBootClasspath()); } if (compileOptions.getExtensionDirs() != null) { args.add("-extdirs"); args.add(compileOptions.getExtensionDirs()); } if (compileOptions.getCompilerArgs() != null) { args.addAll(compileOptions.getCompilerArgs()); } }
private String getDefaultOrCompilerEncoding(CompileOptions compileOptions) { String compilerEncoding = compileOptions != null ? compileOptions.getEncoding() : null; return compilerEncoding == null ? Charset.defaultCharset().name() : compilerEncoding; }
public void configureFor(@Nonnull final DependencyConfig pDepConfig, @Nonnull final SourceSet pSourceSetToCompile, final boolean pIsTest) { final Project project = getProject(); final JavaVersion javaLevel = pDepConfig.getJavaLevel(); setDescription(buildUtil.getLongName() + ": Compile sources from '" + pSourceSetToCompile.getName() + "' source set using dependency configuration '" + pDepConfig.getName() + "' (Java level: " + javaLevel + ")"); // Additional Task Input: the dependency configuration file getInputs().file(pDepConfig.getConfigFile()); final CompileOptions options = getOptions(); options.setEncoding(StandardCharsets.UTF_8.toString()); options.setDeprecation(true); // The warning "Supported source version 'RELEASE_6' from annotation processor 'org.antlr.v4.runtime.misc. // NullUsageProcessor' less than -source '1.7'" is okay and may be ignored. It goes away when Checkstyle // updates to ANTLR 4.5: https://github.com/antlr/antlr4/issues/487 (which they did in version 6.5) final JavaLevelUtil javaLevelUtil = new JavaLevelUtil(project); if (javaLevelUtil.isOlderSupportedJava(javaLevel)) { options.setFork(true); options.getForkOptions().setExecutable(javaLevelUtil.getCompilerExecutable(javaLevel)); } final File destDir = calculateDestDirFromSourceSet(pSourceSetToCompile, pDepConfig.getName()); setSource(pSourceSetToCompile.getJava()); setDestinationDir(destDir); setSourceCompatibility(javaLevel.toString()); setTargetCompatibility(javaLevel.toString()); //setDependencyCacheDir(new File(project.getBuildDir(), "dependency-cache")); FileCollection cp = null; if (pIsTest) { cp = new ClasspathBuilder(project).buildClassPath(pDepConfig, null, false, pSourceSetToCompile, buildUtil.getSourceSet(SourceSet.MAIN_SOURCE_SET_NAME), buildUtil.getSourceSet(BuildUtil.SONARQUBE_SOURCE_SET_NAME)); } else { cp = new ClasspathBuilder(project).buildClassPath(pDepConfig, null, false, pSourceSetToCompile); } setClasspath(cp); if (pIsTest) { Task mainClassesTask = buildUtil.getTask(TaskNames.mainClasses, pDepConfig); Task sqClassesTask = buildUtil.getTask(TaskNames.sonarqubeClasses, pDepConfig); dependsOn(mainClassesTask, sqClassesTask); } }
CompileOptions getCompileOptions();
Compiler<JavaCompileSpec> create(CompileOptions options);
Compiler<JavaCompileSpec> createForJointCompilation(CompileOptions options);