io.ultreia.java4all.config.plugin.DescribeMojo Maven / Gradle / Ivy
Show all versions of config-maven-plugin Show documentation
package io.ultreia.java4all.config.plugin;
/*
* #%L
* Config :: Maven plugin
* %%
* Copyright (C) 2016 - 2018 Code Lutin, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import com.google.common.base.Joiner;
import io.ultreia.java4all.config.ApplicationConfigHelper;
import io.ultreia.java4all.config.ApplicationConfigProvider;
import io.ultreia.java4all.config.io.spi.ConfigModel;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* Describe application config from java files to the description configuration format.
*
* Created on 28/08/16.
*
* @author Tony Chemit - [email protected]
* @since 3.0
*/
@Mojo(name = "describe", requiresDirectInvocation = true, defaultPhase = LifecyclePhase.COMPILE, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
@Execute(phase = LifecyclePhase.COMPILE)
public class DescribeMojo extends ConfigMojoSupport {
/**
* The name of provider to describe.
*
* By default, will use artifactId in camelCase format ( example: provider name for artifact id
* {@code config-example} is {@code ConfigExample} ).
*
* @see ApplicationConfigProvider
*/
@Parameter(property = "providerName")
private String providerName;
private ConfigModel configModel;
@Override
protected void init() throws Exception {
super.init();
if (providerName == null) {
List artifactIdPaths = new ArrayList<>();
for (String artifactIdPath : getProject().getArtifactId().replaceAll("-", ".").split("\\.")) {
artifactIdPaths.add(StringUtils.capitalize(artifactIdPath));
}
providerName = Joiner.on("").join(artifactIdPaths) + "Config";
}
if (isVerbose()) {
getLog().info("Use provider name: " + providerName);
}
ClassLoader loader = initClassLoader(getProject(), new File(getProject().getBuild().getOutputDirectory()), true, false, true, true, true);
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
ApplicationConfigProvider provider;
try {
Thread.currentThread().setContextClassLoader(loader);
provider = ApplicationConfigHelper.getProvider(loader, providerName);
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
if (provider == null) {
throw new MojoExecutionException("Could not find provider with name: " + providerName);
}
String modelName = StringUtils.removeEnd(provider.getName(), "Config");
if (isVerbose()) {
getLog().info("Use model name: " + modelName);
}
if (getModelFile() == null) {
setModelFile(modelName);
}
Files.createDirectories(getModelFile().getParentFile().toPath());
configModel = provider.toModel(Locale.FRANCE);
}
@Override
protected void doAction() throws Exception {
getLog().info("Generate file to: " + getModelFile());
getConfigWriter().write(configModel, getModelFile().toPath());
}
}