create a Groovy script distribution/executable via Maven - java

I have a project module full of groovy scripts which are run via embedded IntelliJ Groovy shell. In a new issue I need to have one of those scripts being run in combination with crontab. Needless to say I cannot just run groovy myScript.groovy dev to have this script executed out of the box - no the dependencies are missing for sure.
I now need a way to have this one particular groovy script being compiled and ready to run out of the box (with the use of the "dev" parameter)
Assuming that I put the myScript.groovy into a directory
main/
|_src/
|_groovy/
What do I need to have a maven build creating a usable executable for me to drop into my machine and let crontab run it accordingly.
I tried a lot of Maven Plugins - but never came far enough. Also I'm sure that there must be a way more trivial way to achieve this since it's a simple build operation in my opinion.

Related

Run a script after JSweet transpilation

I'm trying to build a TypeScript project with JSweet. After the transpilation, I want to run a script in any language to move some files around; specifically files that are already in TypeScript that don't need transpilation. I wrote a Groovy script that does just that but I can't make it run after the generate-sources instructions for JSweet.
Here's my pom.xml file: https://pastebin.com/932r9cWw
I am far from a Maven expert. I think the script shows that I'm trying to run the script scripts/addJsScripts.groovy after the transpilation, but I'm clearly doing something very wrong. The goals available to Maven Invoker plugin don't match the goals of the JSweet transpiler at all.
Is there a way to do what I'm trying to do?
There are plenty of ways to achieve this, but this is more a Maven issue than a JSweet one.
I suggest you use the exec-maven-plugin, as explained here: I want to execute shell commands from Maven's pom.xml

How to run job-dsl-plugin locally with additional plugins

We are using the Jenkins Job-DSL plugin for creating a number of jobs and having the actual job-configuration as part of the version-controlled source-code.
In order to test the resulting XML files locally, I currently use something like the following:
java -jar /opt/job-dsl-plugin/job-dsl-core/build/libs/job-dsl-core-1.78-SNAPSHOT-standalone.jar create_jobs.groovy
This allows to look at the resulting XML while making changes.
However some DSL elements are failing in the local build, but still work on the actual Jenkins installation.
E.g. "batchFile", "pullRequestBuildTrigger" and a few others.
As far as I understand these are separate Jenkins plugins which contribute some additional elements to the DSL, so the core job-dsl-plugin does not know about them.
I tried various ways of adding the code from these plugins to the job-dsl-plugin so that I can run the local transformation, but I could not find a way that actually works. Adding the plugins to job-dsl-plugin, classpath, ... nohting fixed it.
I also looked at How to import and run 3rd party Jenkins Plugin's extension DSL (githubPullRequest) with Gradle tool locally?, but the suggestions there did not work for me as I do not want to run a local Jenkins instance here.
So how can I run the job-dsl-plugin manually with DSL from additional plugins being available?

IntelliJ maven vs normal maven

I would like to add my own commands to delete a specific folder in the mvn repository whenever I run a maven command.
For using maven through the command prompt, this is quite easy since we just update apache-maven-3.5.3\bin\mvn.cmd .
However, I noticed that when we run mvn from intelliJ Maven projects Tool Window, the command run is the following:
C:\mbakOrg\Oracle\JDK\jdk1.8.0_60\bin\java -Dmaven.multiModuleProjectDirectory=
C:\mbakOrg\_CODE\MNE_ARCHIT_GIT\_REPOS\sg-template-store -Dmaven.home=C:\mbakOrg\build\apache-maven-3.5.3 -
Dclassworlds.conf=C:\mbakOrg\build\apache-maven-3.5.3\bin\m2.conf "-javaagent:C:\mbakOrg\devel\JetBrains\IntelliJ IDEA
2017.1.3\lib\idea_rt.jar=42633:C:\mbakOrg\devel\JetBrains\IntelliJ IDEA 2017.1.3\bin" -Dfile.encoding=UTF-8 -classpath
C:\mbakOrg\build\apache-maven-3.5.3\boot\plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2017.1.3 clean install
So how will I add a command that will run every time?
Since maven in InteliJis using a custom way to run maven.
Explanation
My problem is basically that the mvn -U command does not properly pull the newest code all the time. Additionally, we are all using a snapshot of a parent project that is being updated quite often to fix issues.
I would strongly discourage modification of mvn.cmd. Even if you figure out how to do it in command line, and in InteliJ, then think about moving to some kind of Continuous Integration framework, like Jenkins for example, which will use default mvn.cmd?
If there is no possibility to achieve what you want with existing Maven tools, I would recommend writing own Maven plugin, (see this tutorial), and put required functionality there. It will guarantee, that this particular piece of code will be executed in all the environments, and this is the way to make sure, that the command will be launched every time.

Using maven in scripts or adding a script-runner task at the end

I'm using maven on both mac and linux to build a .war file for a website. I'd like to know the best way to automatically run a script that will deploy the website to the server after a build.
What I am currently doing is I have a deploy.sh script that will run
mvn -P<PROFILE> clean package
and will then do a bunch of ssh / scp stuff to copy the target/file.war to the web server and run a bunch of commands to start/stop tomcat - clean out the logs etc.
Problems
Although various stack posts say using $? is supposed to catch the error code from maven I have yet to get it working and if for some reason I have a bad maven build I have no way to detect it. I would not like to do all my deploy tasks if the build fails.
Options?
1) Is there a correct way to detect a bad "build" from maven and have my script abort (I guess i could check if the war doesn't exist ...??)
2) Is there a maven "plugin" that will actually handle this for me, and if so could somebody provide a small code example.
I would do two things in the shell script which calls Maven and the deployment commands:
test whether the WAR file exists before attempting to deploy it, as you suggest yourself;
save Maven output to a timestamped log file for reference.
You may want to consider using maven as the entry point to the deployment and let it call deploy.sh rather than the other way around. This way maven will fail the build if something goes wrong.
The Ant plugin should be able to help with this, take a look at the second example here. It allows you to run a script and fail on error if desired.

Do i have a chance to run a user defined program to generate resource?

I have a war project with maven ,
And I wonder if there is a plugin OR such a mechanism to generate resources at compile time so that I can minify CSS or minify JS or generate CSS-sprite ?
I think this should be a very nice plugin.
I've not used it myself, but the invoker plugin appears to be what you need. It gives you the ability to run scripts pre and post-build. For your case, just use a pre-build script to run whatever utility you need.

Categories