Intellij: Running jar from maven dependancy - java

I am new to maven, I have some .hbs (handle-bar template files), I have "handlebars-proto", which includes premade handlebar server.
https://github.com/jknack/handlebars.java (search for handlebars-proto)
I've added the dependency to my pom.xml as
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars-proto</artifactId>
<version>${current-version}</version>
</dependency>
Documentation states, I should be able to run it using,
java -jar handlebars-proto-${current-version}.jar -dir myTemplates
How can I create a Intellij Run Configuration to do same as above. I tried creating JAR task runner & using "handlevar-proto-{version}.jar" but it's unable to find the jar file. In general how to run a jar from the maven in IntelliJ.

There is option in run configurations to configure JAR Application

Related

Develop ImageJ plugin with openCV in a maven project "ClassNotFoundException" while pack the project to Jar and run it as a plugin in ImageJ

The dependecy I use in my prject is
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>4.5.1-2</version>
</dependency>
I load the library in my class by calling
nu.pattern.OpenCV.loadShared();
The plugins.config content is:
Plugins > Analyze, "Process Pixels", aaa.imagej.Process_Pixels
Everything works fine until I use mvn package to pack the project as a jar file and put it to the imageJ plugin folder. When I run it in the imageJ, it says
"imagej.Process_Pixels class not found"
Notice that it should be aaa.image.Process_Pixels.
However, when I remove "nu.pattern.OpenCV.loadShared();" The plugin sucessfully run but the function with openCV fails for sure.
Is there any alternative way I can try to load the opencv?

Use a locally stored .jar file as a dependency in a Maven project: compile class files

I'm a relative Maven novice and am having difficulties using a locally stored jar as a module within my IntelliJ project - a project I have taken from an online tutorial.
I've brought it into my .m2 folder using:
mvn install:install-file "-Dfile=C:/../resources/myshop-automatedtestscore 3.1.17-SNAPSHOT.jar" "-DpomFile=C:/../resources/myshop-automatedtestscore-3.1.17-SNAPSHOT.pom --Dsources=C:/../myshop-automatedtestscore-3.1.17-SNAPSHOT-sources.jar"
.jar is located at:
C:\Users\daveb\.m2\repository\com\myshop\automatedtests\myshop-automatedtestscore\3.1.7-SNAPSHOT\3.1.17-SNAPSHOT.jar
And I added the dependency in the main pom.xml as follows:
<dependency>
<groupId>com.myshop.automatedtests</groupId>
<artifactId>myshop-automatedtestscore</artifactId>
<version>3.1.17-SNAPSHOT</version>
</dependency>
When I go to Project Structure -> Libraries, I can see the Sources dependency jar file is there in grey so should be fine. However the core project seems to be unable to access the class versions of the file. In Target folder they remain .class but in External libraries they are .java
Apologies if this is a novice or obvious solution. I'm trying to resolve.
You have to install jar using below command -
mvn install:install-file -Dfile= -DgroupId= -DartifactId= -Dversion= -Dpackaging=
You have to give artifacts and other parameters so that you will be able to use in pom file using below code -
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>1.1.GA</version>
</dependency>
After that you have to complie your project so that this dependency will be added in your project.
mvn eclipse:eclipse
mvn clean install
You can press here to force refresh the plugins in Intelij.
Here you can check which repository you have and also which maven you have.
Click on preferences
Another option you have is to perform this :
mvn clean install -U
If your dependency is
<groupId>com.me.example</groupId>
<artifactId>my-example</artifactId>
<version>1.1.0</version>
Then your jar file would be my-example-1.1.0.jar and it will be under \.m2\repository\com\me\example\my-example\1.1.0\my-example-1.1.0.jar.
In your case, your jar file is myshop-automatedtestscore-3.1.17-SNAPSHOT.jar and it should be under
C:\Users\daveb.m2\repository\com\myshop\automatedtests\myshop-automatedtestscore\3.1.17-SNAPSHOT\myshop-automatedtestscore-3.1.17-SNAPSHOT.jar.
After this, do a Maven > Reimport from IntelliJ.

How to build the jar artifact for a Spring Boot project in IntelliJ IDEA

I'm using IntelliJ IDEA 2016.2.5 and I've used Spring Initializr to setup a Spring Boot project. Everything works fine and I can run and test my project in the IDE.
Right now, when I want to build the jar file, I run the following command from command prompt:
$ mvn package
where my pom.xml is located and the jar file is created perfectly (with a caveat that I'm going to tell you about it later). The thing is, I'm looking for a way to do the same from within the IDE.
So far I've tried to create a new jar artifact in Project Structure window (Alt+Ctrl+Shift+s) and include the project's output while for the maven dependencies I use Extract into Output Root. This way, the artifact can be built and run but there are lots of error messages given out and some of the functionalities are not working.
Does anyone know how to build a jar file for Spring Boot project from within IntelliJ IDEA?
The reason why I need to build my jar file in IDE is that my project is composed of several maven projects and I've imported them all as modules into one project. When I run the main module in the IDE, IntelliJ IDEA is smart enough to consider the module as the dependencies for the main one. But when I use the command prompt mvn, this is not happening (which is totally understandable since maven only knows of main project and not the rest). If only I can build from within the IDE..
To clarify my situation better, I've got a "Main" project and a "Library" which of course Library is a dependency of Main. Since I don't have maven repository (and at this point, I'm not looking for one), the only way I know of to make it possible for the maven to compile my code is to name Library in Main's pom.xml like this:
<dependency>
<groupId>org.example</groupId>
<artifactId>library</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${basedir}/lib/library-1.0-SNAPSHOT.jar</systemPath>
</dependency>
which as far as I know is highly discouraged. This is in the case that if I name the Library as an ordinary dependency:
<dependency>
<groupId>org.example</groupId>
<artifactId>library</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
since a neighboring module (Library) outputs the exact same artifact, IntelliJ IDEA is capable of matching the dependency with that module and compile the code. But in case of mvn, since there's no module involved, this can not be done and the dependency is not satisfied. In other words, I can only config my environment to work with mvn or IntelliJ IDEA, but not both!
Go to the project root folder such as springbootdemo and run below commands:
mvnw clean package (This will create the target folder and generate the jar inside it)
mvnw spring-boot:run (This will run the spring boot application).
In my case application name is springbootdemo and its location is C:\Users\springbootdemo and I ran both commands at this location.
From within IDEA, you can go in the tool window "Maven Project", and here double click on Package.

Including excluded jars in intellij run configurations

I have a hadoop job that gets deployed to Amazon EMR. Because EMR provides a hadoop-core jar, I've configured the dependency in my pom as provided:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>0.20.2-cdh3u6</version>
<scope>provided</scope>
</dependency>
The problem is that when I want to debug the jar within intellij, it can't find one of the classes in this jar, org.apache.hadoop.util.RunJar.
Is it possible to configure Intellij to include this jar on the classpath when debugging/running the jar within intellij, but still to exclude this jar from the output jar?
The path to the actual jar on my local machine is /Users/jbrown/.m2/repository/org/apache/hadoop/hadoop-core/0.20.2-cdh3u6/hadoop-core-0.20.2-cdh3u6.jar - I've tried adding -cp /Users/jbrown/.m2/repository/org/apache/hadoop/hadoop-core/0.20.2-cdh3u6/hadoop-core-0.20.2-cdh3u6.jar to the VM options in the run config, but then the job couldn't run at all.
Assuming that you are using maven from your dependency example, you can use multiple maven profiles to tackle this problem.
You can have a prod and debug profiles which include a dependencies section. The debug one can skip the provided tag for all hadoop related dependencies, which would include the dependencies in the final JAR.
You can read more about maven profiles here.

Apache Nifi failure due to java.lang.NoClassDefFoundError in a local maven dependency jar.

This is the first time I am using maven, I want to implement a processor for apache-nifi. Now for this I am using a proprietary jar file which is an SDK. It is not on the repositories. Therefore i have put it in the pom.xml as follows.
<dependency>
<groupId>KS</groupId>
<artifactId>En-SDK</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/En-SDK-Java.jar</systemPath>
</dependency>
I am using intelliJ which seem to have no problem importing this dependency. So is the maven build procedure.
I use mvn clean install as explained in this tutorial. Which gives me with a nar package as the output. (no build errors)
This nar , which is supposed to be put in the $NIFI_HOME/lib directory does not bundle the said local jar.
If i place this nar file in the required directory and start apache-nifi,
INFO [main] org.apache.nifi.nar.NarClassLoaders Loaded NAR file: /..././work/nar/extensions/myNar.nar
Seems to be loaded by the NarClassLoaders , but following that I get an exception and nifi doesn't start.
java.lang.NoClassDefFoundError: com/kls/../../SubscriptionInterface
The SubscriptionInterface is a class from the said local jar.
If I look at the nar file's folder structure, inside the META-INF/bundled-dependencies/ i see every other dependant jar files defined in the pom but not this local jar i used.
How to overcome this?
The following Stackoverflow question seems to be similar to what you have asked.
Maven 2 assembly with dependencies: jar under scope "system" not included
Try to put the dependency in a local repository and declare it in the pom.

Categories