Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ _Baseline is a family of Gradle plugins for configuring Java projects with sensi
| `com.palantir.baseline-config` | Config files for the above plugins
| `com.palantir.baseline-reproducibility` | Sensible defaults to ensure Jar, Tar and Zip tasks can be reproduced
| `com.palantir.baseline-exact-dependencies` | Ensures projects explicitly declare all the dependencies they rely on, no more and no less
| `com.palantir.baseline-encoding` | Ensures projects use the UTF-8 encoding in compile tasks.
| `com.palantir.baseline-release-compatibility` | Ensures projects targetting older JREs only compile against classes and methods available in those JREs.
| `com.palantir.baseline-testing` | Configures test tasks to dump heap dumps (hprof files) for convenient debugging

Expand Down Expand Up @@ -365,6 +366,10 @@ checkImplicitDependencies {
}
```

## com.palantir.baseline-encoding

This plugin sets the encoding for JavaCompile tasks to `UTF-8`.

## com.palantir.baseline-release-compatibility

This plugin adds the `--release <number>` flag to JavaCompile tasks (when the compiler [supports it](https://openjdk.java.net/jeps/247)), so that published jars will only use methods available in the target JRE. Relying on `sourceCompatibility = 1.8` and `targetCompatibility = 1.8` is insufficient because you run the risk of using method that have been added in newer JREs, e.g. `Optional#isEmpty`.
Expand Down
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-1600.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: feature
feature:
description: Baseline now provides a `com.palantir.baseline-encoding` plugin to
force UTF-8 in compilation tasks.
links:
- https://github.com/palantir/gradle-baseline/pull/1600
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void apply(Project project) {
proj.getPluginManager().apply(BaselineIdea.class);
proj.getPluginManager().apply(BaselineErrorProne.class);
proj.getPluginManager().apply(BaselineFormat.class);
proj.getPluginManager().apply(BaselineEncoding.class);
proj.getPluginManager().apply(BaselineReproducibility.class);
proj.getPluginManager().apply(BaselineClassUniquenessPlugin.class);
proj.getPluginManager().apply(BaselineExactDependencies.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.baseline.plugins;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.tasks.compile.JavaCompile;

public final class BaselineEncoding implements Plugin<Project> {

@Override
public void apply(Project project) {
project.getTasks().withType(JavaCompile.class).configureEach(javaCompileTask -> {
javaCompileTask.getOptions().setEncoding("UTF-8");
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implementation-class=com.palantir.baseline.plugins.BaselineEncoding
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.baseline


import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome

class BaselineEncodingIntegrationTest extends AbstractPluginTest {

def standardBuildFile = '''
plugins {
id 'java'
id 'com.palantir.baseline-encoding'
}

sourceCompatibility = 1.8

repositories {
mavenLocal()
jcenter()
}
'''.stripIndent()

def otherEncodingBuildFile = '''
plugins {
id 'java'
}

sourceCompatibility = 1.8

repositories {
mavenLocal()
jcenter()
}

tasks.withType(JavaCompile) {
options.encoding = 'US-ASCII'
}
'''.stripIndent()

def javaFile = '''
package test;
public class Test {
private static final String VALUE = "•";
}
'''.stripIndent()

def 'compileJava succeeds with baseline-encoding'() {
when:
buildFile << standardBuildFile
file('src/main/java/test/Test.java').text = javaFile

then:
BuildResult result = with('compileJava').build()
result.task(":compileJava").outcome == TaskOutcome.SUCCESS
!result.output.contains("unmappable character")
}

def 'compileJava fails with other encoding'() {
when:
buildFile << otherEncodingBuildFile
file('src/main/java/test/Test.java').text = javaFile

then:
BuildResult result = with('compileJava').build()
result.task(":compileJava").outcome == TaskOutcome.SUCCESS
result.output.contains("unmappable character")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compilation doesn't actually fail when encountering unmappable characters - those characters are just replaced with the unicode replacement character.

}
}