Skip to content

Commit

Permalink
Add automation of Properties creator in build phase with a maven plugin
Browse files Browse the repository at this point in the history
Feature to automate the properties file creation;
Created a plugin that can be add in the pom to automatic create properties based on passed args
  • Loading branch information
lucalas committed May 31, 2018
1 parent d9497a4 commit 3cdde0f
Show file tree
Hide file tree
Showing 7 changed files with 688 additions and 0 deletions.
67 changes: 67 additions & 0 deletions owner-creator-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.aeonbits.owner</groupId>
<artifactId>maven-plugin-owner-creator</artifactId>
<packaging>maven-plugin</packaging>
<version>1.0.11-SNAPSHOT</version>
<name>OWNER :: Plugin</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aeonbits.owner</groupId>
<artifactId>owner</artifactId>
<version>1.0.11-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>2.0.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<configuration>
<!-- see http://jira.codehaus.org/browse/MNG-5346 -->
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>

<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2012-2015, Luigi R. Viggiano
* All rights reserved.
*
* This software is distributable under the BSD license.
* See the terms of the BSD license in the documentation provided with this software.
*/

package org.aeonbits.owner.plugin;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import org.aeonbits.owner.creator.PropertiesFileCreator;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;

/**
*
* @author Luca Taddeo
*/

@Mojo(name = "create", requiresDependencyResolution = ResolutionScope.RUNTIME)
public class PropertiesFileCreatorMojo
extends AbstractMojo {

/**
* Location of the output properties file.
*/
@Parameter(required=true)
private String outputDirectory;

/**
* Config class to parse.
*/
@Parameter(required=true)
private String packageClass;

/**
* Jar to include.
*/
@Parameter
private String jarPath;

/**
* Project Name.
*/
@Parameter
private String projectName;

/**
* Project description.
*/
@Parameter
private String projectDesription;

@Component
private MavenProject project;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {

List<URL> urls = new ArrayList();

if(jarPath != null && !jarPath.isEmpty()) {
System.out.println("Use passed jar [" + jarPath + "]");
File jar = new File(jarPath);
urls.add(jar.toURI().toURL());
} else {
System.out.println("Use classpath");
// Loading project classpath to retrieve class
for (Object ele : project.getRuntimeClasspathElements()) {
File jar = new File(ele.toString());
urls.add(jar.toURI().toURL());
}
}

URLClassLoader jarPack = new URLClassLoader(urls.toArray(new URL[urls.size()]), this.getClass().getClassLoader());
Class classToLoad = jarPack.loadClass(packageClass);

// Parse class and create property file
PropertiesFileCreator creator = new PropertiesFileCreator();
creator.parse(classToLoad, outputDirectory, projectName, projectDesription);
System.out.println("Conversion succeded, properties saved [" + outputDirectory + "]");
} catch (Throwable ex) {
throw new MojoExecutionException(ex.getMessage());
}
}

}
50 changes: 50 additions & 0 deletions owner/src/main/java/org/aeonbits/owner/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,56 @@ public interface Config extends Serializable {
Class<? extends Decryptor> value() default IdentityDecryptor.class;
}

/**
* The description that appears in created property file.
*/
@Retention(RUNTIME)
@Target(METHOD)
@Documented
@interface Description {
String value();
}

/**
* If this annotation is put on a property, it will not write on property file.
*/
@Retention(RUNTIME)
@Target(METHOD)
@Documented
@interface NoProperty {
}

/**
* Value to assign to the property in the property file.
*/
@Retention(RUNTIME)
@Target(METHOD)
@Documented
@interface ValorizedAs {
String value();
}

/**
* Order to follow for groups in property file.
*/
@Retention(RUNTIME)
@Target(TYPE)
@Documented
@interface GroupOrder {
String[] value();
}

/**
* Group of a property.
* Can be more than one as group and subgroup.
*/
@Retention(RUNTIME)
@Target(METHOD)
@Documented
@interface Group {
String[] value();
}

/**
* Specifies the policy type to use to load the {@link org.aeonbits.owner.Config.Sources} files for properties.
*
Expand Down
18 changes: 18 additions & 0 deletions owner/src/main/java/org/aeonbits/owner/creator/Creator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2012-2015, Luigi R. Viggiano
* All rights reserved.
*
* This software is distributable under the BSD license.
* See the terms of the BSD license in the documentation provided with this software.
*/

package org.aeonbits.owner.creator;

/**
* Interface to convert Config class into something else.
*
* @author Luca Taddeo
*/
public interface Creator {
boolean parse(Class clazz, String output, String headerName, String projectName) throws Exception;
}
Loading

0 comments on commit 3cdde0f

Please sign in to comment.