Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to disable rewriting log4j.properties #220

Merged
merged 4 commits into from
Jul 5, 2021
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Added

* [#201](https://github.com/xenit-eu/alfresco-docker-gradle-plugin/issues/201) - Make it possible to disable rewriting log4j.properties

## Version 5.3.0 - 2021-04-21

**This release drops support for Gradle versions before 5.6**
Expand Down
17 changes: 17 additions & 0 deletions docs/02-plugin-docker-alfresco.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,23 @@ baseAlfrescoWar -+--------------------------------------------------------------

</details>

### Disabling log4j modifications

It is possible to disable the `prefixAlfrescoLog4j` or the `prefixShareLog4j` tasks using standard Gradle task management features, like [`enabled`](https://docs.gradle.org/current/dsl/org.gradle.api.Task.html#org.gradle.api.Task:enabled) or [`onlyIf`](https://docs.gradle.org/current/dsl/org.gradle.api.Task.html#org.gradle.api.Task:onlyIf(groovy.lang.Closure)).

```groovy
prefixAlfrescoLog4j {
enabled = false
}

prefixShareLog4j {
onlyIf { myHatColor == "purple" }
}
```

> **Warning:** This feature is only supported on Gradle 6.2 and later.
> It is not possible to disable these tasks on earlier Gradle versions.

## Example

An example for the usage of this plugin can be found in the [applyamps example](../src/integrationTest/examples/applyamps-example).
Expand Down
58 changes: 58 additions & 0 deletions src/integrationTest/examples/log4j-prefix-disable/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
plugins {
id "eu.xenit.docker-alfresco"
}


repositories {
mavenCentral()
maven {
url "https://artifacts.alfresco.com/nexus/content/groups/public/"
}
}

dependencies {
baseAlfrescoWar "org.alfresco:content-services-community:6.0.7-ga@war"
}

prefixAlfrescoLog4j {
enabled = false
}

dockerBuild {
alfresco {
baseImage = "tomcat:7-jre8"
}
}

// Check if no log4j changes are applied
import com.bmuschko.gradle.docker.tasks.container.*

task createContainer(type: DockerCreateContainer) {
imageId = buildDockerImage.imageId
// Just run a command so the container stays active in the background without starting Alfresco
cmd = ["bash", "-c", "while true; do sleep 100; done"]
dependsOn(buildDockerImage)
hostConfig.autoRemove = true
}

task startContainer(type: DockerStartContainer) {
containerId = createContainer.containerId
dependsOn(createContainer)
}

task checkDefaultLog4J(type: DockerExecContainer) {
containerId = createContainer.containerId
withCommand(["fgrep", "File", "/usr/local/tomcat/webapps/alfresco/WEB-INF/classes/log4j.properties"])
successOnExitCodes.add(0)
dependsOn(startContainer)
}

task terminateContainer(type: DockerKillContainer) {
containerId = createContainer.containerId
dependsOn(createContainer)
}

task functionalTest {
dependsOn(checkDefaultLog4J)
finalizedBy(terminateContainer)
}
55 changes: 55 additions & 0 deletions src/integrationTest/examples/log4j-prefix/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
plugins {
id "eu.xenit.docker-alfresco"
}


repositories {
mavenCentral()
maven {
url "https://artifacts.alfresco.com/nexus/content/groups/public/"
}
}

dependencies {
baseAlfrescoWar "org.alfresco:content-services-community:6.0.7-ga@war"
}

dockerBuild {
alfresco {
baseImage = "tomcat:7-jre8"
}
}


// Check if log4j changes are applied correctly
import com.bmuschko.gradle.docker.tasks.container.*

task createContainer(type: DockerCreateContainer) {
imageId = buildDockerImage.imageId
// Just run a command so the container stays active in the background without starting Alfresco
cmd = ["bash", "-c", "while true; do sleep 100; done"]
dependsOn(buildDockerImage)
hostConfig.autoRemove = true
}

task startContainer(type: DockerStartContainer) {
containerId = createContainer.containerId
dependsOn(createContainer)
}

task checkLog4JApplied(type: DockerExecContainer) {
containerId = createContainer.containerId
withCommand(["fgrep", "[ALFRESCO]", "/usr/local/tomcat/webapps/alfresco/WEB-INF/classes/log4j.properties"])
successOnExitCodes.add(0)
dependsOn(startContainer)
}

task terminateContainer(type: DockerKillContainer) {
containerId = createContainer.containerId
dependsOn(createContainer)
}

task functionalTest {
dependsOn(checkLog4JApplied)
finalizedBy(terminateContainer)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package eu.xenit.gradle.testrunner;

import static org.junit.Assume.assumeTrue;

import org.gradle.util.GradleVersion;
import org.junit.Test;

import java.io.IOException;
Expand Down Expand Up @@ -98,4 +101,16 @@ public void publishWar() throws IOException {
testProjectFolder(EXAMPLES.resolve("publish-war"), ":publishToMavenLocal");
}

@Test
public void log4jPrefix() throws IOException {
testProjectFolder(EXAMPLES.resolve("log4j-prefix"), ":functionalTest");
}

@Test
public void log4jPrefixDisable() throws IOException {
assumeTrue("Gradle 6.2 is required to disable prefixLog4j tasks",
WimCrols marked this conversation as resolved.
Show resolved Hide resolved
GradleVersion.version(gradleVersion).compareTo(GradleVersion.version("6.2")) > 0);
testProjectFolder(EXAMPLES.resolve("log4j-prefix-disable"), ":functionalTest");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,6 @@ private static List<TaskProvider<? extends WarLabelOutputTask>> warEnrichmentCha

final List<TaskProvider<? extends WarEnrichmentTask>> tasks = new ArrayList<>();

tasks.add(project.getTasks()
.register("prefix" + warName + "Log4j", PrefixLog4JWarTask.class,
prefixLog4JWarTask -> prefixLog4JWarTask.getPrefix().set(warName.toUpperCase()))
);

tasks.add(project.getTasks()
.register("apply" + warName + "SM", InjectFilesInWarTask.class, injectFilesInWarTask -> {
injectFilesInWarTask.getTargetDirectory().set("/WEB-INF/lib/");
Expand All @@ -176,6 +171,11 @@ private static List<TaskProvider<? extends WarLabelOutputTask>> warEnrichmentCha
.from(project.getConfigurations().named(warName.toLowerCase() + "Amp"));
}));

tasks.add(project.getTasks()
WimCrols marked this conversation as resolved.
Show resolved Hide resolved
.register("prefix" + warName + "Log4j", PrefixLog4JWarTask.class,
prefixLog4JWarTask -> prefixLog4JWarTask.getPrefix().set(warName.toUpperCase()))
);

for (TaskProvider<? extends WarEnrichmentTask> taskProvider : tasks) {
taskProvider.configure(task -> {
task.dependsOn(resolveTask);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.xenit.gradle.docker.alfresco.tasks;

import eu.xenit.gradle.docker.internal.GradleVersionRequirement;
import java.util.Map;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.RegularFileProperty;
Expand All @@ -24,7 +25,10 @@ public abstract class AbstractWarEnrichmentTask extends DefaultTask implements W

protected AbstractWarEnrichmentTask() {
outputWar.set(inputWar.flatMap(_x -> getProject().getLayout().getBuildDirectory()
.file("xenit-gradle-plugins/" + getName() + "/" + getName() + ".war")));
.file("xenit-gradle-plugins/" + getName() + "/" + getName() + ".war"))
.map(outputFile -> isEnabled() ? outputFile
Copy link
Contributor

Choose a reason for hiding this comment

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

SInce I'm unfamiliar with the SDK, I am probably missing something, but won't this change result in always throwing when on version < 6.2, while the intention is to only warn when you want to disable the task?

Copy link
Member Author

Choose a reason for hiding this comment

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

What we do here is setting the outputWar property to null when this task is disabled.

This signals to the tasks that consume the output war (so createDockerFile & alfrescoWar) that there is no file to add.

We do not want to warn when disabling the task on < 6.2, we want to actively fail the build with a clear error message.

Returning null from a map() results in a vague exception in Gradle versions before 6.2. The x ? y : z operator is equivalent to if(x) { return y; } else { return z; }, the non-matching side will not be evaluated at all.

On the other hand, keeping the output file when the task is disabled will result in an exception from the tasks that consume the output war, because the promised output file does not actually exists (not generated, because the task is not run).

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for the thorough explanation.

: GradleVersionRequirement.atLeast("6.2", "disable the " + getName() + " task", () -> null))
);
}

@InputFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public MergeWarsTask() {
.set(getProject().getLayout().getBuildDirectory().dir("xenit-gradle-plugins/" + getName()));
getArchiveBaseName().set(getName());
childWars = getRootSpec().addChildBeforeSpec(getMainSpec());
childWars.setDuplicatesStrategy(DuplicatesStrategy.EXCLUDE);
childWars.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage;
import eu.xenit.gradle.docker.compose.DockerComposePlugin;
import eu.xenit.gradle.docker.internal.Deprecation;
import eu.xenit.gradle.docker.internal.GradleVersionRequirement;
import java.io.File;
import java.util.UUID;
import org.gradle.api.Action;
Expand All @@ -17,7 +18,6 @@
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.util.GradleVersion;

/**
* Created by thijs on 10/24/16.
Expand All @@ -31,11 +31,8 @@ public class DockerConfigPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
if (GradleVersion.current().compareTo(GradleVersion.version("5.6")) < 0) {
throw new GradleException(
"The " + PLUGIN_ID + " plugin requires at least Gradle 5.6. You are running "
+ GradleVersion.current());
}
GradleVersionRequirement.atLeast("5.6", "use the " + PLUGIN_ID + " plugin");

// Set up deprecation warnings
Deprecation.setStartParameter(project.getGradle().getStartParameter());
project.getGradle().projectsEvaluated(g -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package eu.xenit.gradle.docker.internal;

import java.util.function.Supplier;
import org.gradle.util.GradleVersion;

public final class GradleVersionRequirement {

private GradleVersionRequirement() {
}

static class UnsupportedGradleVersion extends UnsupportedOperationException {

private UnsupportedGradleVersion(GradleVersion requiredVersion, GradleVersion currentVersion, String feature) {
super(requiredVersion.toString() + " or later is required" + (feature == null ? ""
: " to " + feature) + ". You are currently using " + currentVersion.toString());
}
}

public static void atLeast(GradleVersion gradleVersion, String feature) {
if (GradleVersion.current().compareTo(gradleVersion) < 0) {
throw new UnsupportedGradleVersion(gradleVersion, GradleVersion.current(), feature);
}
}

public static void atLeast(String gradleVersion, String feature) {
atLeast(GradleVersion.version(gradleVersion), feature);
}

public static <T> T atLeast(GradleVersion gradleVersion, String feature, Supplier<T> callable) {
atLeast(gradleVersion, feature);
return callable.get();
}

public static <T> T atLeast(String gradleVersion, String feature, Supplier<T> callable) {
atLeast(gradleVersion, feature);
return callable.get();
}
}