Gradle logging plugin for easier logging in subprojects.
This commit is contained in:
parent
4cf74da120
commit
31c099b6fb
@ -17,6 +17,10 @@ gradlePlugin {
|
|||||||
id = 'GroowtAntlrPlugin'
|
id = 'GroowtAntlrPlugin'
|
||||||
implementationClass = 'groowt.gradle.antlr.GroowtAntlrPlugin'
|
implementationClass = 'groowt.gradle.antlr.GroowtAntlrPlugin'
|
||||||
}
|
}
|
||||||
|
create('groowtLogging') {
|
||||||
|
id = 'groowt-logging'
|
||||||
|
implementationClass = 'groowt.gradle.logging.GroowtLoggingPlugin'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
//file:noinspection GrPackage
|
||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java'
|
||||||
}
|
}
|
||||||
@ -8,7 +9,6 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
testImplementation libs.junit.jupiter.api
|
testImplementation libs.junit.jupiter.api
|
||||||
testRuntimeOnly libs.log4j.slf4jBinding
|
|
||||||
}
|
}
|
||||||
|
|
||||||
java {
|
java {
|
||||||
@ -17,8 +17,6 @@ java {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: hook into testResources and add a default log4j2.xml config for testing
|
|
||||||
|
|
||||||
testing {
|
testing {
|
||||||
suites {
|
suites {
|
||||||
test {
|
test {
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
package groowt.gradle.logging
|
||||||
|
|
||||||
|
import org.gradle.api.tasks.Copy
|
||||||
|
|
||||||
|
class GroowtCopyLoggerConfigTask extends Copy {}
|
@ -0,0 +1,54 @@
|
|||||||
|
package groowt.gradle.logging
|
||||||
|
|
||||||
|
import org.gradle.api.Plugin
|
||||||
|
import org.gradle.api.Project
|
||||||
|
import org.gradle.api.artifacts.VersionCatalogsExtension
|
||||||
|
import org.gradle.api.plugins.JavaPluginExtension
|
||||||
|
|
||||||
|
class GroowtLoggingPlugin implements Plugin<Project> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void apply(Project project) {
|
||||||
|
def javaExtension = project.extensions.getByType(JavaPluginExtension)
|
||||||
|
|
||||||
|
def defaultLog4j2XmlResource = this.class.getResource('default-log4j2.xml')
|
||||||
|
if (defaultLog4j2XmlResource == null) {
|
||||||
|
throw new RuntimeException('default-log4j2.xml is null')
|
||||||
|
}
|
||||||
|
|
||||||
|
def tmpLog4j2Xml = File.createTempFile('default-log4j2-xml', 'tmp')
|
||||||
|
defaultLog4j2XmlResource.withReader { reader ->
|
||||||
|
tmpLog4j2Xml.withWriter {
|
||||||
|
reader.transferTo(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
javaExtension.sourceSets.each { sourceSet ->
|
||||||
|
project.tasks.register(
|
||||||
|
sourceSet.getTaskName('copy', 'LoggingConfig'),
|
||||||
|
GroowtCopyLoggerConfigTask
|
||||||
|
) { task ->
|
||||||
|
task.from(tmpLog4j2Xml)
|
||||||
|
task.rename { 'log4j2.xml' }
|
||||||
|
task.into(['src', sourceSet.name, 'resources'].join(File.separator))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def libs = project.extensions.getByType(VersionCatalogsExtension).named('libs')
|
||||||
|
|
||||||
|
project.dependencies.addProvider(
|
||||||
|
'implementation', libs.findLibrary('slf4j-api').orElseThrow()
|
||||||
|
)
|
||||||
|
project.dependencies.addProvider(
|
||||||
|
'runtimeOnly', libs.findLibrary('log4j-core').orElseThrow()
|
||||||
|
)
|
||||||
|
project.dependencies.addProvider(
|
||||||
|
'runtimeOnly', libs.findLibrary('log4j-slf4jBinding').orElseThrow()
|
||||||
|
)
|
||||||
|
|
||||||
|
project.tasks.named('build') {
|
||||||
|
it.dependsOn project.tasks.withType(GroowtCopyLoggerConfigTask)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">
|
||||||
|
<Appenders>
|
||||||
|
<Console name="root">
|
||||||
|
<PatternLayout>
|
||||||
|
<LevelPatternSelector defaultPattern="[%t] %-5level %logger{1.} %msg%n">
|
||||||
|
<PatternMatch key="DEBUG" pattern="[%t] %-5level %logger{1.}.%M() %msg%n"/>
|
||||||
|
</LevelPatternSelector>
|
||||||
|
</PatternLayout>
|
||||||
|
</Console>
|
||||||
|
</Appenders>
|
||||||
|
<Loggers>
|
||||||
|
<Root level="INFO">
|
||||||
|
<AppenderRef ref="root"/>
|
||||||
|
</Root>
|
||||||
|
</Loggers>
|
||||||
|
</Configuration>
|
@ -1,5 +1,6 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'GroowtConventions'
|
id 'GroowtConventions'
|
||||||
|
id 'groowt-logging'
|
||||||
id 'com.jessebrault.jbarchiva' version '0.1.0'
|
id 'com.jessebrault.jbarchiva' version '0.1.0'
|
||||||
id 'maven-publish'
|
id 'maven-publish'
|
||||||
}
|
}
|
||||||
@ -14,7 +15,15 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation libs.gradle.tooling, libs.picocli, project(':groowt-gradle-model')
|
[
|
||||||
|
libs.gradle.tooling,
|
||||||
|
libs.picocli,
|
||||||
|
libs.slf4j.api,
|
||||||
|
libs.log4j.core,
|
||||||
|
project(':groowt-gradle-model')
|
||||||
|
].each { implementation it }
|
||||||
|
|
||||||
|
runtimeOnly libs.log4j.slf4jBinding
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named('jar', Jar) {
|
tasks.named('jar', Jar) {
|
||||||
|
17
cli/src/main/java/groowt/cli/FileAndPathUtil.java
Normal file
17
cli/src/main/java/groowt/cli/FileAndPathUtil.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package groowt.cli;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public final class FileAndPathUtil {
|
||||||
|
|
||||||
|
public static File packageNameToFile(String packageName) {
|
||||||
|
return new File(packageName.replace(".", File.separator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static File resolve(File from, File to) {
|
||||||
|
return from.toPath().resolve(to.toPath()).toFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
private FileAndPathUtil() {}
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,5 @@
|
|||||||
package groowt.cli;
|
package groowt.cli;
|
||||||
|
|
||||||
import groowt.gradle.model.GroowtGradleModel;
|
|
||||||
import picocli.CommandLine;
|
import picocli.CommandLine;
|
||||||
import picocli.CommandLine.Command;
|
import picocli.CommandLine.Command;
|
||||||
|
|
||||||
@ -39,13 +38,13 @@ public class Generate implements Callable<Integer> {
|
|||||||
@Override
|
@Override
|
||||||
public Integer call() {
|
public Integer call() {
|
||||||
if (this.componentName != null) {
|
if (this.componentName != null) {
|
||||||
GradleUtil.doWith(this.cli.getProjectDir(), project -> {
|
GradleUtil.doWithGroowtGradleModel(this.cli.getProjectDir(), model -> {
|
||||||
final var model = project.getModel(GroowtGradleModel.class);
|
if (this.sourceDir == null) {
|
||||||
if (sourceDir == null) {
|
|
||||||
this.sourceDir = new File(String.join(File.separator, "src", this.sourceSet, "groovy"));
|
this.sourceDir = new File(String.join(File.separator, "src", this.sourceSet, "groovy"));
|
||||||
}
|
}
|
||||||
final File packageDir = new File(
|
final File packageDir = FileAndPathUtil.resolve(
|
||||||
this.sourceDir, model.getBasePackage().replace(".", File.separator)
|
this.sourceDir,
|
||||||
|
FileAndPathUtil.packageNameToFile(model.getBasePackage())
|
||||||
);
|
);
|
||||||
packageDir.mkdirs();
|
packageDir.mkdirs();
|
||||||
final File componentFile = new File(packageDir, this.componentName + ".txt");
|
final File componentFile = new File(packageDir, this.componentName + ".txt");
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package groowt.cli;
|
package groowt.cli;
|
||||||
|
|
||||||
|
import groowt.gradle.model.GroowtGradleModel;
|
||||||
import org.gradle.tooling.GradleConnector;
|
import org.gradle.tooling.GradleConnector;
|
||||||
import org.gradle.tooling.ProjectConnection;
|
import org.gradle.tooling.ProjectConnection;
|
||||||
|
|
||||||
@ -8,13 +9,24 @@ import java.util.function.Consumer;
|
|||||||
|
|
||||||
public final class GradleUtil {
|
public final class GradleUtil {
|
||||||
|
|
||||||
public static void doWith(File projectDir, Consumer<ProjectConnection> action) {
|
public static void doWith(File projectDir, Consumer<? super ProjectConnection> action) {
|
||||||
final var gradleConnector = GradleConnector.newConnector().forProjectDirectory(projectDir);
|
final var gradleConnector = GradleConnector.newConnector().forProjectDirectory(projectDir);
|
||||||
try (final var projectConnection = gradleConnector.connect()) {
|
try (final var projectConnection = gradleConnector.connect()) {
|
||||||
action.accept(projectConnection);
|
action.accept(projectConnection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T> void doWith(File projectDir, Class<? extends T> modelClass, Consumer<? super T> modelConsumer) {
|
||||||
|
doWith(projectDir, projectConnection -> {
|
||||||
|
final T model = projectConnection.getModel(modelClass);
|
||||||
|
modelConsumer.accept(model);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void doWithGroowtGradleModel(File projectDir, Consumer<? super GroowtGradleModel> modelConsumer) {
|
||||||
|
doWith(projectDir, GroowtGradleModel.class, modelConsumer);
|
||||||
|
}
|
||||||
|
|
||||||
private GradleUtil() {}
|
private GradleUtil() {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package groowt.cli;
|
package groowt.cli;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import picocli.CommandLine;
|
import picocli.CommandLine;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -13,6 +15,8 @@ import java.io.File;
|
|||||||
)
|
)
|
||||||
public class GroowtCli {
|
public class GroowtCli {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(GroowtCli.class);
|
||||||
|
|
||||||
@CommandLine.Option(
|
@CommandLine.Option(
|
||||||
names = { "-v", "--verbose" },
|
names = { "-v", "--verbose" },
|
||||||
description = "Log verbosely to standard out."
|
description = "Log verbosely to standard out."
|
||||||
@ -27,7 +31,7 @@ public class GroowtCli {
|
|||||||
private File projectDir;
|
private File projectDir;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("Hello from Groowt!");
|
logger.info("Hello from Groowt! Version 0.1.0");
|
||||||
System.exit(new CommandLine(new GroowtCli()).execute(args));
|
System.exit(new CommandLine(new GroowtCli()).execute(args));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,13 +10,13 @@ version = '0.1.0'
|
|||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
maven {
|
// maven {
|
||||||
url 'https://repo.gradle.org/gradle/libs-releases'
|
// url 'https://repo.gradle.org/gradle/libs-releases'
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation libs.groovy, libs.gradle.tooling, project(':groowt-gradle-model')
|
implementation libs.groovy, project(':groowt-gradle-model')
|
||||||
}
|
}
|
||||||
|
|
||||||
gradlePlugin {
|
gradlePlugin {
|
||||||
@ -34,6 +34,5 @@ publishing {
|
|||||||
artifactId = 'groowt-gradle'
|
artifactId = 'groowt-gradle'
|
||||||
from components.java
|
from components.java
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user