Skip to content
New issue

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

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

Already on GitHub? # to your account

Added Camel support #22

Merged
merged 1 commit into from
Aug 17, 2024
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Gradle plugin for easy [MapStruct](https://mapstruct.org/) setup
Usage:
```groovy
plugins {
id 'com.github.akazver.mapstruct' version '1.0.7'
id 'com.github.akazver.mapstruct' version '1.0.8'
}
```

Expand All @@ -31,6 +31,11 @@ plugins {
- [mapstruct-spring-extensions](https://mvnrepository.com/artifact/org.mapstruct.extensions.spring/mapstruct-spring-extensions) (annotationProcessor)
- [mapstruct-spring-test-extensions](https://mvnrepository.com/artifact/org.mapstruct.extensions.spring/mapstruct-spring-test-extensions) (testImplementation)

**Camel** (optional)
- [camel-mapstruct](https://mvnrepository.com/artifact/org.apache.camel/camel-mapstruct) (implementation)
- [camel-mapstruct-starter](https://mvnrepository.com/artifact/org.apache.camel.springboot/camel-mapstruct-starter) (implementation)
- [camel-quarkus-mapstruct](https://mvnrepository.com/artifact/org.apache.camel.quarkus/camel-quarkus-mapstruct) (implementation)

## Config
Plugin adds configuration block which looks like this:
```groovy
Expand Down
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = 'com.github.akazver.mapstruct'
version = '1.0.7'
version = '1.0.8'

gradlePlugin {
website = 'https://github.com/AkaZver'
Expand All @@ -20,7 +20,7 @@ gradlePlugin {
displayName = 'MapStruct Plugin'
description = 'Automatic MapStruct configuration'
implementationClass = 'com.github.akazver.gradle.plugins.mapstruct.MapstructPlugin'
tags = ['mapstruct']
tags = ['mapstruct', 'mapping', 'lombok', 'spring', 'quarkus', 'camel']
}
}
}
Expand All @@ -33,10 +33,10 @@ repositories {
dependencies {
testImplementation 'io.freefair.gradle:lombok-plugin:8.7.1'
testImplementation 'org.assertj:assertj-core:3.26.3'
testImplementation 'org.mockito:mockito-core:5.12.0'
testImplementation 'org.mockito:mockito-junit-jupiter:5.12.0'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.11.0'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.11.0'
testImplementation 'org.mockito:mockito-core:5.12.0'
testImplementation 'org.mockito:mockito-junit-jupiter:5.12.0'
}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
package com.github.akazver.gradle.plugins.mapstruct;

import com.github.akazver.gradle.plugins.mapstruct.manager.CompilerArgsManager;
import com.github.akazver.gradle.plugins.mapstruct.manager.DependencyManager;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.compile.CompileOptions;
import org.gradle.api.tasks.compile.JavaCompile;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.github.akazver.gradle.plugins.mapstruct.PluginDependency.*;
import org.jetbrains.annotations.NotNull;

/**
* Plugin implementation which adds necessary dependencies and compiler options
Expand All @@ -28,110 +13,18 @@
*/
public class MapstructPlugin implements Plugin<Project> {

private static final Logger LOGGER = Logging.getLogger(MapstructPlugin.class);
private static final String ADDING_MESSAGE = "Adding {} dependencies";
private static final String COMPILER_ARG_PATTERN = "-Amapstruct.%s=%s";

@Override
public void apply(Project project) {
public void apply(@NotNull Project project) {
DependencyManager dependencyManager = new DependencyManager(project);
CompilerArgsManager compilerArgsManager = new CompilerArgsManager(project);

project.getExtensions().create("mapstruct", MapstructExtension.class);

project.afterEvaluate(it -> {
addRequiredDependencies(it);
addOptionalDependencies(it);
addCompilerArgs(it);
dependencyManager.addRequiredDependencies();
dependencyManager.addOptionalDependencies();
compilerArgsManager.addCompilerArgs();
});
}

private void addDependencies(Project project, List<PluginDependency> pluginDependencies) {
DependencyHandler projectDependencies = project.getDependencies();

pluginDependencies.forEach(pluginDependency -> {
LOGGER.lifecycle("- {}", pluginDependency.getId());
projectDependencies.add(pluginDependency.getConfiguration(), pluginDependency.getId());
});
}

private void addRequiredDependencies(Project project) {
LOGGER.lifecycle(ADDING_MESSAGE, "MapStruct");
addDependencies(project, MAPSTRUCT_DEPENDENCIES);
}

private void addOptionalDependencies(Project project) {
ConfigurationContainer configurations = project.getConfigurations();

if (hasLombok(configurations) && !hasBinding(configurations)) {
LOGGER.lifecycle(ADDING_MESSAGE, "Lombok");
addDependencies(project, LOMBOK_DEPENDENCIES);
}

if (hasSpring(configurations)) {
LOGGER.lifecycle(ADDING_MESSAGE, "Spring");
addDependencies(project, SPRING_DEPENDENCIES);
}
}

private boolean hasLombok(ConfigurationContainer configurations) {
return hasConfiguration(configurations, "lombok")
|| hasDependency(configurations, LOMBOK);
}

private boolean hasBinding(ConfigurationContainer configurations) {
return hasDependency(configurations, LOMBOK_MAPSTRUCT_BINDING);
}

private boolean hasSpring(ConfigurationContainer configurations) {
return hasConfiguration(configurations, "bootArchives")
|| hasDependency(configurations, SPRING_CORE);
}

private boolean isNeededDependency(Dependency dependency, PluginDependency pluginDependency) {
return pluginDependency.getGroup().equals(dependency.getGroup())
&& pluginDependency.getName().equals(dependency.getName());
}

private boolean hasDependency(ConfigurationContainer configurations, PluginDependency pluginDependency) {
return configurations.getByName(pluginDependency.getConfiguration())
.getAllDependencies()
.stream()
.anyMatch(dependency -> isNeededDependency(dependency, pluginDependency));
}

private boolean hasConfiguration(ConfigurationContainer configurations, String configurationName) {
return configurations.findByName(configurationName) != null;
}

private void addCompilerArgs(Project project) {
MapstructExtension extension = project.getExtensions().getByType(MapstructExtension.class);

CompileOptions compileOptions = project.getTasks()
.withType(JavaCompile.class)
.getByName("compileJava")
.getOptions();

Stream<String> projectCompilerArgs = compileOptions.getCompilerArgs().stream();

Stream<String> mapstructCompilerArgs = Arrays.stream(MapstructExtension.class.getDeclaredFields())
.filter(field -> !field.isSynthetic())
.map(Field::getName)
.map(name -> fetchCompilerArg(extension, name));

List<String> compilerArgs = Stream.concat(projectCompilerArgs, mapstructCompilerArgs)
.collect(Collectors.toList());

compileOptions.setCompilerArgs(compilerArgs);
}

private String fetchCompilerArg(MapstructExtension extension, String name) {
try {
PropertyDescriptor descriptor = new PropertyDescriptor(name, MapstructExtension.class);
Object value = descriptor.getReadMethod().invoke(extension);

return String.format(COMPILER_ARG_PATTERN, name, value);
} catch (IllegalAccessException | InvocationTargetException | IntrospectionException exception) {
String message = String.format("Can't fetch compiler argument for '%s'", name);
throw new MapstructPluginException(message, exception);
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.akazver.gradle.plugins.mapstruct.dependency;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

/**
* Dependencies added by the plugin
*
* @author Vasiliy Sobolev
*/
@Getter
@ToString
@SuppressWarnings("java:S1192")
@EqualsAndHashCode(callSuper = true)
public class AdditionalDependency extends PluginDependency {

public static final AdditionalDependency MAPSTRUCT =
new AdditionalDependency("implementation", "org.mapstruct:mapstruct:1.6.0");

public static final AdditionalDependency MAPSTRUCT_PROCESSOR =
new AdditionalDependency("annotationProcessor", "org.mapstruct:mapstruct-processor:1.6.0");

public static final AdditionalDependency LOMBOK_MAPSTRUCT_BINDING =
new AdditionalDependency("annotationProcessor", "org.projectlombok:lombok-mapstruct-binding:0.2.0");

public static final AdditionalDependency MAPSTRUCT_SPRING_EXTENSIONS =
new AdditionalDependency("annotationProcessor", "org.mapstruct.extensions.spring:mapstruct-spring-extensions:1.1.1");

public static final AdditionalDependency MAPSTRUCT_SPRING_ANNOTATIONS =
new AdditionalDependency("implementation", "org.mapstruct.extensions.spring:mapstruct-spring-annotations:1.1.1");

public static final AdditionalDependency MAPSTRUCT_SPRING_TEST_EXTENSIONS =
new AdditionalDependency("testImplementation", "org.mapstruct.extensions.spring:mapstruct-spring-test-extensions:1.1.1");

public static final AdditionalDependency CAMEL_MAPSTRUCT =
new AdditionalDependency("implementation", "org.apache.camel:camel-mapstruct:4.7.0");

public static final AdditionalDependency CAMEL_MAPSTRUCT_STARTER =
new AdditionalDependency("implementation", "org.apache.camel.springboot:camel-mapstruct-starter:4.7.0");

public static final AdditionalDependency CAMEL_QUARKUS_MAPSTRUCT =
new AdditionalDependency("implementation", "org.apache.camel.quarkus:camel-quarkus-mapstruct:3.13.0");

public AdditionalDependency(String configuration, String id) {
super(configuration, id);
}

}
Loading