Search in sources :

Example 1 with CompileOptions

use of org.gradle.api.tasks.compile.CompileOptions in project gradle by gradle.

the class JavaCompilerArgumentsBuilder method addMainOptions.

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.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.isDebug()) {
        if (compileOptions.getDebugOptions().getDebugLevel() != null) {
            args.add("-g:" + compileOptions.getDebugOptions().getDebugLevel().trim());
        } else {
            args.add("-g");
        }
    } else {
        args.add("-g:none");
    }
    FileCollection sourcepath = compileOptions.getSourcepath();
    if (!noEmptySourcePath || sourcepath != null && sourcepath.isEmpty()) {
        args.add("-sourcepath");
        args.add(sourcepath == null ? "" : sourcepath.getAsPath());
    }
    if (spec.getSourceCompatibility() == null || JavaVersion.toVersion(spec.getSourceCompatibility()).compareTo(JavaVersion.VERSION_1_6) >= 0) {
        List<File> annotationProcessorPath = spec.getAnnotationProcessorPath();
        if (annotationProcessorPath == null || annotationProcessorPath.isEmpty()) {
            args.add("-proc:none");
        } else {
            args.add("-processorpath");
            args.add(Joiner.on(File.pathSeparator).join(annotationProcessorPath));
        }
    }
    /*This is an internal option, it's used in com.sun.tools.javac.util.Names#createTable(Options options). The -XD backdoor switch is used to set it, as described in a comment
        in com.sun.tools.javac.main.RecognizedOptions#getAll(OptionHelper helper). This option was introduced in JDK 7 and controls if compiler's name tables should be reused.
        Without this option being set they are stored in a static list using soft references which can lead to memory pressure and performance deterioration
        when using the daemon, especially when using small heap and building a large project.
        Due to a bug (https://builds.gradle.org/viewLog.html?buildId=284033&tab=buildResultsDiv&buildTypeId=Gradle_Master_Performance_PerformanceExperimentsLinux) no instances of
        SharedNameTable are actually ever reused. It has been fixed for JDK9 and we should consider not using this option with JDK9 as not using it  will quite probably improve the
        performance of compilation.
        Using this option leads to significant performance improvements when using daemon and compiling java sources with JDK7 and JDK8.*/
    args.add(USE_UNSHARED_COMPILER_TABLE_OPTION);
}
Also used : CompileOptions(org.gradle.api.tasks.compile.CompileOptions) FileCollection(org.gradle.api.file.FileCollection) File(java.io.File)

Example 2 with CompileOptions

use of org.gradle.api.tasks.compile.CompileOptions in project gradle by gradle.

the class JdkJavaCompiler method createCompileTask.

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);
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) CompileOptions(org.gradle.api.tasks.compile.CompileOptions)

Example 3 with CompileOptions

use of org.gradle.api.tasks.compile.CompileOptions in project gradle-apt-plugin by tbroyer.

the class AptPlugin method apply.

@Override
public void apply(final Project project) {
    configureCompileTasks(project, JavaCompile.class, new GetCompileOptions<JavaCompile>() {

        @Override
        public CompileOptions getCompileOptions(JavaCompile task) {
            return task.getOptions();
        }
    });
    configureCompileTasks(project, GroovyCompile.class, new GetCompileOptions<GroovyCompile>() {

        @Override
        public CompileOptions getCompileOptions(GroovyCompile task) {
            return task.getOptions();
        }
    });
    project.getPlugins().withType(JavaBasePlugin.class, new Action<JavaBasePlugin>() {

        @Override
        public void execute(JavaBasePlugin javaBasePlugin) {
            final JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
            javaConvention.getSourceSets().all(new Action<SourceSet>() {

                @Override
                public void execute(final SourceSet sourceSet) {
                    AptSourceSetConvention convention = IMPL.createAptSourceSetConvention(project, sourceSet);
                    new DslObject(sourceSet).getConvention().getPlugins().put(PLUGIN_ID, convention);
                    AptSourceSetOutputConvention outputConvention = new AptSourceSetOutputConvention(project);
                    outputConvention.setGeneratedSourcesDir(new File(project.getBuildDir(), "generated/source/apt/" + sourceSet.getName()));
                    new DslObject(sourceSet.getOutput()).getConvention().getPlugins().put(PLUGIN_ID, outputConvention);
                    ensureConfigurations(project, sourceSet, convention);
                    configureCompileTaskForSourceSet(project, sourceSet, sourceSet.getCompileJavaTaskName(), JavaCompile.class, new GetCompileOptions<JavaCompile>() {

                        @Override
                        public CompileOptions getCompileOptions(JavaCompile task) {
                            return task.getOptions();
                        }
                    });
                }
            });
        }
    });
    project.getPlugins().withType(GroovyBasePlugin.class, new Action<GroovyBasePlugin>() {

        @Override
        public void execute(GroovyBasePlugin groovyBasePlugin) {
            JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
            javaConvention.getSourceSets().all(new Action<SourceSet>() {

                @Override
                public void execute(SourceSet sourceSet) {
                    AptSourceSetConvention convention = new DslObject(sourceSet).getConvention().getPlugin(AptSourceSetConvention.class);
                    configureCompileTaskForSourceSet(project, sourceSet, sourceSet.getCompileTaskName("groovy"), GroovyCompile.class, new GetCompileOptions<GroovyCompile>() {

                        @Override
                        public CompileOptions getCompileOptions(GroovyCompile task) {
                            return task.getOptions();
                        }
                    });
                }
            });
        }
    });
}
Also used : Action(org.gradle.api.Action) DslObject(org.gradle.api.internal.plugins.DslObject) GroovyCompile(org.gradle.api.tasks.compile.GroovyCompile) CompileOptions(org.gradle.api.tasks.compile.CompileOptions) SourceSet(org.gradle.api.tasks.SourceSet) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) GroovyBasePlugin(org.gradle.api.plugins.GroovyBasePlugin) JavaBasePlugin(org.gradle.api.plugins.JavaBasePlugin) JavaCompile(org.gradle.api.tasks.compile.JavaCompile) File(java.io.File)

Aggregations

CompileOptions (org.gradle.api.tasks.compile.CompileOptions)3 File (java.io.File)2 JavaCompiler (javax.tools.JavaCompiler)1 StandardJavaFileManager (javax.tools.StandardJavaFileManager)1 Action (org.gradle.api.Action)1 FileCollection (org.gradle.api.file.FileCollection)1 DslObject (org.gradle.api.internal.plugins.DslObject)1 GroovyBasePlugin (org.gradle.api.plugins.GroovyBasePlugin)1 JavaBasePlugin (org.gradle.api.plugins.JavaBasePlugin)1 JavaPluginConvention (org.gradle.api.plugins.JavaPluginConvention)1 SourceSet (org.gradle.api.tasks.SourceSet)1 GroovyCompile (org.gradle.api.tasks.compile.GroovyCompile)1 JavaCompile (org.gradle.api.tasks.compile.JavaCompile)1