-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RequireJavaVendor rule. Fixes #19
- Loading branch information
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
core/enforcer-rules/src/main/groovy/enforcer/rules/RequireJavaVendor.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2020-2021 The author and/or original authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 enforcer.rules | ||
|
||
import groovy.transform.CompileStatic | ||
import org.gradle.api.model.ObjectFactory | ||
import org.gradle.api.provider.ListProperty | ||
import org.kordamp.gradle.plugin.enforcer.api.EnforcerContext | ||
import org.kordamp.gradle.plugin.enforcer.api.EnforcerPhase | ||
import org.kordamp.gradle.plugin.enforcer.api.EnforcerRuleException | ||
|
||
import javax.inject.Inject | ||
|
||
import static org.kordamp.gradle.plugin.enforcer.api.EnforcerPhase.AFTER_PROJECT | ||
import static org.kordamp.gradle.plugin.enforcer.api.EnforcerPhase.BEFORE_BUILD | ||
|
||
/** | ||
* This rule checks that the Java vendor is allowed. | ||
* | ||
* Adapted from {@code org.apache.maven.plugins.enforcer.RequireJavaVendor} | ||
* Original author: Tim Sijstermans | ||
* | ||
* @author Andres Almiray | ||
* @since 0.9.0 | ||
*/ | ||
@CompileStatic | ||
class RequireJavaVendor extends AbstractStandardEnforcerRule { | ||
final ListProperty<String> includes | ||
final ListProperty<String> excludes | ||
|
||
@Inject | ||
RequireJavaVendor(ObjectFactory objects) throws EnforcerRuleException { | ||
super(objects, [BEFORE_BUILD, AFTER_PROJECT] as EnforcerPhase[]) | ||
includes = objects.listProperty(String).convention([]) | ||
excludes = objects.listProperty(String).convention([]) | ||
} | ||
|
||
void include(String str) { | ||
includes.add(str) | ||
} | ||
|
||
void exclude(String str) { | ||
excludes.add(str) | ||
} | ||
|
||
@Override | ||
protected void doExecute(EnforcerContext context) throws EnforcerRuleException { | ||
String javaVendor = System.getProperty('java.vendor') | ||
|
||
if (excludes.get().contains(javaVendor)) { | ||
if (!includes.get().empty) { | ||
if (!includes.get().contains(javaVendor)) { | ||
throw createException(javaVendor) | ||
} | ||
return | ||
} | ||
throw createException(javaVendor) | ||
} | ||
} | ||
|
||
private EnforcerRuleException createException(String javaVendor) { | ||
StringBuilder sb = new StringBuilder() | ||
|
||
if (message.present) { | ||
sb.append(message.get()) | ||
.append(System.lineSeparator()) | ||
} | ||
sb.append("Vendor ${javaVendor} is in a list of banned vendors: ${excludes.get()}") | ||
|
||
fail(sb.toString()) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
docs/guide/src/docs/asciidoc/rules/require-java-vendor.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
= RequireJavaVendor | ||
|
||
This rule enforces that the Java vendor is allowed. | ||
|
||
== Allowed Phases | ||
* BEFORE_BUILD | ||
* AFTER_PROJECT | ||
|
||
== Default Phases | ||
* BEFORE_BUILD | ||
* AFTER_PROJECT | ||
|
||
== Configuration | ||
[source,groovy] | ||
[subs="+macros"] | ||
---- | ||
enforce { | ||
rule(enforcer.rules.RequireJavaVendor) { r -> | ||
r.enabled | ||
r.enforcerLevel | ||
r.message | ||
r.phases | ||
r.excludes | ||
r.includes | ||
} | ||
} | ||
---- | ||
|
||
== Properties | ||
|
||
[%header, cols="<,<,<,^,<4"] | ||
|=== | ||
| Name | ||
| Type | ||
| Default | ||
| Required | ||
| Description | ||
|
||
| excludes | ||
| ListProperty<String> | ||
| `[ ]` | ||
| {icon_req_n} | ||
| Specify the banned vendors. This should be an exact match of the System Property `java.vendor`. | ||
|
||
| includes | ||
| ListProperty<String> | ||
| `[ ]` | ||
| {icon_req_n} | ||
| Specify the allowed vendor names. This should be an exact match of the System Property `java.vendor`. | ||
|
||
|=== | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters