Create executable JAR from Groovy script - maven

I have a maven project which consist of 1 groovy script (src/main/groovy/Main.groovy)
I am able to run it from IntelliJ by simply clicking run. What I want to do is to create an executable jar containing all dependencies so I could run in with
java -jar myjar.jar
Here's my pom:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
</project>
I've searched on google and on so but couldn't find any working solution.

Using Maven you can set up build plugins, and one of these can be a Groovy compiler.
I wrote a blog article on setting up a Groovy compiler in Maven. But there's two parts: the groovy compiler and a plugin to package the .class files into the .jar. Traditionally for the former you'd use the groovy-eclipse-compiler and the maven-shade-plugin for the latter.
It's like 100 lines of XML in your pom file.

Related

Download Maven artifact with version range

I need download specific Maven artifact using version range e.g.:
GroupId:org.apache.logging.log4j
ArtifactId=log4j-api
Version=[2.17.1,)
Right now I need it in one CI job.
How can I do it?
I was hoping to find an easier solution, but at least this works:
Create pom.xml file
<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>cz.vondr</groupId>
<artifactId>maven-dependency-download</artifactId>
<version>1.0.0</version>
<properties>
<dep.group>org.apache.commons</dep.group>
<dep.artifact>commons-lang3</dep.artifact>
<dep.version>3.12.0</dep.version>
<dep.type>jar</dep.type>
<dep.classifier></dep.classifier>
</properties>
<dependencies>
<dependency>
<groupId>${dep.group}</groupId>
<artifactId>${dep.artifact}</artifactId>
<version>${dep.version}</version>
<type>${dep.type}</type>
<classifier>${dep.classifier}</classifier>
</dependency>
</dependencies>
</project>
And then use it to download artifact using command:
mvn dependency:copy-dependencies "-DoutputDirectory=./downloaded-dependencies" -Ddep.group="org.apache.logging.log4j" -Ddep.artifact="log4j-api" -Ddep.version="[2.17.1,)"
Additional information
Basic description is here:
http://vondrnotes.blogspot.com/2022/09/download-maven-artifact-with-version.html
Working example is here:
https://github.com/bugs84/download-maven-dependency-with-version-range

Why doesn't my Jsoup Maven project run?

Hi I am new to Maven and JSoup and trying to run an example in Intellij so I can get an understanding of JSoup.
I added the dependencies of JSoup to my POM.xml file. I pushed to Github when the project was working. As soon as I pulled from Github the project won't run. So I went into the terminal and getting JSoup errors that certain imports don't exist. I tried to 'run' as a Maven project but now I'm even more confused.
Please see the image below which demonstrates the file structure. The code is simple its the config / setup that I am really struggling with.
Project Structure
The POM.xml content is seen below:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>WebScraper</groupId>
<artifactId>WebScraper</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<!-- jsoup HTML parser library # http://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
</dependencies>
</project>
Hopefully someone can help with this.
Thanks

mac maven cannot find symbol wrong servlet-api.jar

I compile spring webflow samples project using maven in mac os and got errors. (The project compiled successfully in windows)
So I create a simple maven project to reproduce the error.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>test</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<dependencies>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
src/main/java/Test.java
import javax.servlet.ServletContext;
public class Test {
public void init(ServletContext context) {
context.setInitParameter("javax.faces.DEFAULT_SUFFIX", ".xhtml");
}
}
mvn compile
The error is:
I thought is was something that maven used a wrong servlet-api.jar but not servlet as pom dependency specified which is correct. (I use javac -cp javax.servlet-api-3.0.1.jar Test.java, result no error)
I check mvn dependency:tree, the result show correct servlet-api-3.0.1.jar.
I also chech mvn script to see if there is some option to set classpath.
But I just can't figure out how maven use which jars as it's classpath.
It's very weird.
After I've imported this test maven project into eclipse,
I run mvn compile again, no error happened.

IntelliJ can't find maven dependencies for GAE

I was following along with the Creating a Simple Hello World Backend API tutorial (GAE) and all was running fine via local terminal but as soon as I opened the project in IntelliJ, IntelliJ can't build the project...
The pom file has a bunch of errors about being unable to find any dependencies, however I can still build the application just fine outside of IntelliJ.
I'm not overly familiar with Maven but I believe it is configured correctly in the IDE as I can build other generic examples generated by IntelliJ. I've attempted to re-import all dependencies but no luck...
Is there something else I need to do here?
Snippet from the pom file where both dependencies are failing to find versions.
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>com.example.helloworld</groupId>
<artifactId>helloworld</artifactId>
<properties>
<app.id>your-app-id</app.id>
<app.version>1</app.version>
<appengine.version>${appengine.version}</appengine.version>
<gcloud.plugin.version>0.9.58.v20150505</gcloud.plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
</properties>
<prerequisites>
<maven>3.1.0</maven>
</prerequisites>
<dependencies>
<!-- Compile/runtime dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>${appengine.version}</version>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-endpoints</artifactId>
<version>${appengine.version}</version>
</dependency>
Your appengine.version property is recursively defined, so IntelliJ can't figure out what it is. Substitute <appengine.version>${appengine.version}</appengine.version> with <appengine.version>1.9.24</appengine.version> and you should be fine.

Maven and dependencies

I have a simply POM like this :
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sim</groupId>
<artifactId>log4j</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.sim</groupId>
<artifactId>sim-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../sim-java/pom.xml</relativePath>
</parent>
<name>log4j</name>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>compile</scope>
</dependency>
</dependencies>
I run mvn clean package on this project and a JAR is created as expected.
When I navigate into this JAR, I thought that I would see a JAR named log4j-1.2.17.jar inside it but it's not the case.
Using dependency with compile scope does not include JAR into packaging version of project ?
Thank you for clarification
The jar:jar plugin of maven just compiles your source and bundles it into a jar. Just like building a jar out of ant or bare hands, no dependency jars will be bundled in the jar. Jars cannot have dependent jars bundled inside them and even if they did, they cannot be loaded by the default class loader.
If you are looking to build a ejb-jar, then you might want to consider a EJB plugin
Check this link for various plugins that you can exploit.

Resources