Using fmpp in gradle to generate java files - gradle

I have an existing maven project which I am attempting to port to gradle.
One sub module uses fmpp/freemarker to generate a whole lot of java files which are then fed back into the build.
I'm new to gradle, and was wondering if anyone knows of an easy way to do this.
Any help would be appreciated.
My current pom.xml looks like this:
<build>
<plugins>
<!-- Freemarker maven plugin for code generation -->
<plugin>
<groupId>com.googlecode.fmpp-maven-plugin</groupId>
<artifactId>fmpp-maven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.unix4j</groupId>
<artifactId>unix4j-tools</artifactId>
<version>0.1-SNAPSHOT</version>
<optional>true</optional>
</dependency>
</dependencies>
<configuration>
<cfgFile>src/main/resources/codegen/config.fmpp</cfgFile>
<outputDirectory>target/generated-sources/main/java</outputDirectory>
<templateDirectory>src/main/resources/codegen/templates</templateDirectory>
</configuration>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/generated</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Sorry, should have spent more time googling first. This is the solution which worked for me:
project(':unix4j-core:unix4j-command') {
configurations {pmd}
dependencies {
compile project(':unix4j-core:unix4j-base')
compile project(':unix4j-tools')
pmd project(':unix4j-tools')
}
task generateFmppSources(dependsOn: ":unix4j-tools:compileJava") << {
println "Generating sources...."
ant.taskdef(name:'fmpp', classname:'fmpp.tools.AntTask', classpath:configurations.pmd.asPath);
ant.fmpp configuration:"src/main/resources/codegen/config.fmpp", sourceRoot:"src/main/resources/codegen/templates", outputRoot:"target/generated-sources/main/java";
}
compileJava.dependsOn generateFmppSources
sourceSets {
main {
java {
srcDir 'target/generated-sources/main/java'
}
}
}
}

Related

Maven remove version from dependency jar

I'd like to know if there is a way to remove version number from maven dependency.
Let's say for my project I'd like to fetch commons-lang3 3.4 using maven dependency plugin:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
My pom configuration says, it is fetching dependencies to the ./lib directory inside of my project.
What I would like to achieve is remove on the fly version number from commons-lang3-3.4.jar. It would look like:
./lib/commons-lang3.jar
Question: Is there any way to do such thing?
Specifying finalName won't help here.
<build>
<finalName>${project.name}-testing</finalName>
</build>
Below my existing configuration:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>process-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${dir.javaLibs}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
To remove the version from copied dependencies, you can use the stripVersion option of the maven-dependency-plugin:
Strip artifact version during copy
Which has default value to false.
Hence, given your existing configuration, here is the change:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>process-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${dir.javaLibs}</outputDirectory>
<!-- new configuration entry below-->
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Spring-Boot and scala cannot find main class

I am trying to make a simple hello world web application in Scala which bootstraps spring-boot as its framework using maven as build tool.
I looked around a bit (there are a few tutorials with Gradle and much less information with maven, is there a reason for it?)
I Managed to get everything wired up but now i am stuck with this error when trying to run the application:
Error: Could not find or load main class SampleWebApplication
Here is the source code,
parent pom from github removing some code about git and deployment leaving the structure intact
Then my web application pom:
<parent>
<groupId>X.Y.Z</groupId>
<artifactId>spring-boot-scala-app-parent</artifactId>
<version>1.0.5-SNAPSHOT</version>
</parent>
<artifactId>webservice</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.1.3.RELEASE</version>
<configuration>
<mainClass>SampleWebApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
SampleWebApplication:
import org.springframework.boot.SpringApplication
class SampleWebApplication extends App {
SpringApplication.run(classOf[SampleConfig]);
}
SampleConfig:
#Configuration
#EnableAutoConfiguration
#ComponentScan
class SampleConfig {
}
Add it inside your build tag
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<phase>process-resources</phase>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<phase>process-test-resources</phase>
</execution>
</executions>
<configuration>
<scalaVersion>2.11.5</scalaVersion>
</configuration>
</plugin>

Can I produce both jar and war of a project in maven?

I have a project(A) in maven that has packaging of war. One other project(B) depends on A and it needs project A jar file but in phase of compile, the war of project A will produce and no jar is available for project B.
How can I create a jar of project A in phase of compile so that project B can use it?
I would suggest to go a different way and use the maven-war-plugin which can produce a separate artifact for the classes which can be used like the following:
<dependency>
<groupId>myGroup</groupId>
<artifactId>myArtifact</artifactId>
<version>myVersion</myVersion>
<classifier>classes</classifier>
</dependency>
This can be achieved by using the following configuration in your war module:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
...
</plugins>
I found the solution : :)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>make-a-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>
${project.build.directory}/${project.artifactId}-${project.version}.jar
</file>
</configuration>
</execution>
</executions>
</plugin>

Obfuscate dependencies to single jar with Proguard

I have modular maven application and "module1" depends on "module2" and "module2" depends on "module3" etc.. In module1 I have something like this:
<profile>
<id>obfuscate</id>
<build>
<plugins>
<plugin>
<groupId>com.pyx4me</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<options>
<option>-allowaccessmodification</option>
<option>-keep public class com.test.Main { *; }</option>
</options>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
</libs>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard</artifactId>
<version>${proguard.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
This creates successfully obfuscated "module1". I want to create single jar with all obfuscated dependencies (obfuscated module1 and module2 etc.). Is it possible?
Maven Shade Plugin
This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.
Check it out here.
Try the <assembly> option of the maven-proguard-plugin. Make references to each of the dependencies you wanted bundled into the obfuscated jar. See examples.
If you want package dependency jars into your uber jar, use shade plugin! here's an example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

Reference resources from a test-jat artifact

I have two artifact:
artifact-A: contains resources in src/test/resources/
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
artifact B: uses resources from artifact A
<dependency>
<groupId>com.xxxx.yyy</groupId>
<artifactId>artifact-A</artifactId>
<version>3.0-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
The problem is that the resources are never extracted in the project artifact-B.
How can I do that ?
If you define a dependency like this the used jar will never be extracted cause it will be put on the classpath during compilation etc. This means to access the resources from artifact-A you need to access them via the classpath.
In artifact-B, I used the maven-dependency-plugin to extract resources from the test-jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>resource-dependencies</id>
<phase>process-test-resources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeArtifactIds>artifact-A</includeArtifactIds>
<includes>**/db-test/*</includes>
<outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

Resources