Read files within maven plugin - maven

I am trying to create a maven-plugin that generates new files based on a template file (basically using the FreeMarker language). I can successfully generate the files if I run the maven plugin from the directory of my maven-plugin project since my plugin accesses the template file based on a relative path.
However, if I try to run the plugin in some other java project directory, I cannot find the template file. I do not want to copy the templated file into the new java project.
I searched around to see if the maven plugin can access files within itself when another project is using the plugin but wasn't successful. Most of the documentation refers to accessing the java project files, and not the maven-plugin files.
Is this even possible or is there a better approach/workaround to tackle the problem?
Edit (Directory structure):
Maven-Plugin (FileGenerator):
src
--main
--java
--FileGenerator.java (references the TemplateFile based on the path to the root project)
TemplateFile
If I run the plugin as a standalone, I am able to generate new files based on the TemplateFile. I achieve the following structure
Maven-Plugin (FileGenerator):
src
--main
--java
--FileGenerator.java
TemplateFile
NewFile1
NewFile2
However, if I run the plugin in another directory (such as part of another java project) with the command mvn myplugin:mypluginplugin:1.0-SNAPSHOT:build
Another Java Project
src
--main
--java
--AnotherFile.java
pom.xml
I get an error mentioning that the TemplateFile cannot be found. Is there a way for the plugin to reference the TemplateFile regardless of where it is run at?
Ideal Output After running plugin
Another Java Project
src
--main
--java
--AnotherFile.java
pom.xml
NewFile1
NewFile2

Related

Jenkins Job - Creating a zip file with war file, appspec.yml and scripts folder

I have created a build with Jenkins for a spring boot application and it is creating a war file.
Now I want to create a second job which should create a zip file with the war file created and appsepc.yml file and a folder "scripts" folder which contains some shell script that the appspec.yml file uses. Can anyone let me know how to do this?
The job name is "Package" so the following is the structure where the different files are.
.jenkins\workspace\Package\target\cpproject.war
.jenkins\workspace\Package\appspec.yml
.jenkins\workspace\Package\scripts\after_install.sh
.jenkins\workspace\Package\scripts\before_install.sh
.jenkins\workspace\Package\scripts\start_server.sh
.jenkins\workspace\Package\scripts\stop_server.sh
Thank you.
See the Maven Assembly Plugin:
The Assembly Plugin for Maven enables developers to combine project output into a single distributable archive that also contains dependencies, modules, site documentation, and other files.
Currently it can create distributions in the following formats:
zip
...

In Maven, how to compile a class outside the source directory into an arbitrary target directory?

I have a legacy app that I'm porting from Ant to Maven. My Maven build works fine for the main project, which I've moved into the standard Maven directory layout (*.java files in /src/main/java/) and it outputs the compiled classes into /target/classes/ as neat as you could wish. These are packaged in a .war file.
However, the project also has a class outside of the folder hierarchy, indeed outside of the web application, that contains scripts that run via cron job. Let's say it's /cronjobs/MyClass.java. I need that class to be compiled and output to /target/cronjobs/MyClass.class and zipped up as part of the resulting .war file, in its /cronjobs/ folder.
Can Maven do this? I know it's possible to change the default "src" directory and "target" directory, but I don't know if (or how) it's possible to run a separate, parallel compile step for just one class.
I can move the source file, of course, if it's easier to compile it with the other classes and then move it later (maybe with the WAR plugin?) but I definitely need the compiled MyClass.class file in the /cronjobs/ directory of the .war.
I'd split the project in 2 parts, webapp as war and cronjobs as jar. Maven knows about multi-module format and it is somewhat the best way to go forward and decouple the webapp from non-webapp code.

How Do You Specify Where The Gradle Wrapper Installs Gradle?

I have an off the shelf application that ships a version of gradle with it. It also has scripts that are hard coded to set GRADLE_HOME to this location.
I want to zip up this dir, put it in nexus and replace it with the gradle wrapper.
How do I configure the gradle wrapper to download this zip from nexus and extract it to a specific location in the project?
EDIT: In the gradle-wrapper.properties I have
distributionPath=wrapper/gradle
However, I end up with it being unzipped to
...\wrapper\gradle\gradle-2.3-bin\8gn7esgljqyucijpbynjk93oc\gradle-2.3
How do I get it to unzip to the path I specified and not to the subdirs?
The location to which Gradle gets unpacked is a combination of the distributionBase and distributionPath properties in gradle-wrapper.properties file. The location specified by distributionPath will always be considered as relative to distributionBase. The only available values for distributionPath are GRADLE_USER_HOME and PROJECT. Even when using PROJECT the wrapper will still generate the folder structure you see above.
If you want to control this more precisely I'd suggest not relying on the wrapper to do this and instead add a task to your build specifically for this purpose.

How do I add a directory in /target to the resulting JAR with Spring Boot?

I'm using Enunciate to generate REST documentation upon building a REST application. Enunicate outputs documentation to /target/docs. I would like to add the /docs directory to the resulting JAR file (and rename it) to be able to serve docs as static content.
How do I do this? I can't figure out how to get these static files (which are generated upon build) into the JAR.
I guess you can solve this by configuring the Maven plugin for enunciate and wiring it up to be run in the 'generate-resources' lifecycle phase.
Also, make sure you set the output-dir to a subdirectory of src/main/resources/static, as commented by Rob above.
I added this to my enunciate.xml to force the docs directory to be generated in a custom location which will be packaged with the .war file
<docs docsDir="target/<app_name>/docs"/>
and then maven will put the entire contents of target/ into the resulting war file package

Create only 1 tar file with mvn package command

I am using maven, testng, selenium and java framework. I have mojo appassembler plugins to create maven build.
I noticed that when I create mvn package - there are few folders getting created under target. for example:
archive-tmp
end2end-1.0-bin.tar
generated-sources
maven-status
classes
end2end-1.0-jar-with-dependencies.jar
generated-test-sources
surefire-reports
end2end-1.0
end2end-1.0.jar
maven-archiver
test-classes
Here, after I untar end2end-1.0.bin.tar, I am able to run the build however it also depends on other folders, classes outside tar build. (inside target folder)
What I want to do is : to create only 1 tar file for entire build. So that other user can copy tar, untar and directly run it.
I know there are some configuration need to set in POM.xml for build. Does anyone have any specific ideas?
Thanks,

Resources