Want to convert it in build.gradle - java

I am very new to gradle, so I need help.
I have a simple bat file with below code.
set cp=%CLASSPATH%;.
set cp=%cp%;.\abc.jar
set cp=%cp%;.\xyz.jar
javac -d classes -cp "%cp%" com\myproject\convert\plugins\plugin1\MyPlugin1.java
jar -cvf CustomPlugin1.jar -C classes .
Now i want to convert this bat file in build.gradle file.
Like plugin1 I have total 10 plugins to build, but i want to build separate jar for each plugin. And every plugin includes a single java file. SO I guess I have to define 10 different tasks in gradle, as i want a provision to build any single plugin whenever required.
So how can I achieve it in an easy way ?

Related

What are auto executable jars?

I was going through spring-boot-maven-plugin documentation and came across a term auto executable jar.
Could someone please explain me what is an auto executable jar and how is it different then normal jar files and how they are auto executed?
spring-boot-maven-plugin documentation mentions the term but does not go further to explain it
repackage: create a jar or war file that is auto-executable. It can replace the regular artifact or can be attached to the build lifecycle with a separate classifier.
Could someone please explain me what is an auto executable jar
A fully executable jar can be executed like any other executable
binary or it can be registered with init.d or systemd. This makes it
very easy to install and manage Spring Boot applications in common
production environments.
So In conclusion is like any other executable when you use a executable jar
how is it different then normal jar files and how they are auto executed?
Well a java file you need to run with java -jar
From Spring Docs
The Maven build of a Springboot application first build your own application and pack it into a JAR file.
In the second stage (repackage) it will wrap that jar with all the jar files from the dependency tree into a new wrapper jar archive. It will also generate a Manifest file where is defined what's the application Main class is (also in the wrapper jar).
After mvn package you can also see 2 jar files in your target directory. The original file and the wrapped jar file.
You can start a Springboot application with a simple command like:
java -jar my-springboot-app.jar
I may suggest that auto executable means that you supplied main method so that it can be launched with java -jar options, otherwise it may be just a java library.
Here is a quote from https://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html
Repackages existing JAR and WAR archives so that they can be executed from the command line using java -jar. With layout=NONE can also be used simply to package a JAR with nested dependencies (and no main class, so not executable).
Executable jar - the one that has main class declared in manifest and can be run with java -jar yourJarFile.jar command
Other jars - jars jars without delcared main calss. Can be anything - application, library, etc. Still can run application by providing fully.qualified.class.name as entry point like java -cp yourJarFile.jar my.bootstrap.BootstrapClass
Autoexecutable jars - never heard about it :)

How to add external library to Hadoop map-reduce task

I have MyClass.java to define the map-reduce task. MyClass.java contains the definition of mapper, reducer and main. It works properly, but if I try to use/add an external jar, I have the message ClassNotFoundException.
To compile I use the command:
javac -classpath hadoop_library_path:my_library_path -sourcepath code_path/ -d class_path/ path/MyClass.java
I create the jar, and then I run the task:
hadoop jar maclass.jar MyClass input output -target target
The external jar need to be added also in in "jar hadoop" command?
I tried with the -libjars option with no result. Any idea?
As I commented, I see two options (there could be more):
Use Eclipse and generate a runnable jar (I am not sure about NetBeans or IntelliJ).
Use maven and its shade plugin to generate an uber jar. You should add all the external libraries that you use as dependencies.
I recommend the latter option.

maven dependency is not available in classpath while running java -jar command [duplicate]

I'm trying to get a maven managed project to run on the command line.
I have a set of dependencies in the pom.xml which are subsequently downloaded and installed in the ~/.m2/repository/. I've included the necessary config in my pom to add the classpath to the jar manifest.
Now the problem is i'm attempting to run the jar thus: java -jar project-SNAPSHOT.jar.
Java can't find the downloaded dependencies (i'm assuming because they are listed without paths in the manifest?) , but i'm not sure how best to get this running.
Options 1:
The jar created does not have the dependent jar files. So, you need to tell java the class-path where all the dependent jars are
java -cp /lcoation/of/dependency1.jar:/location/of/dependency2.jar:/location/of/dependency3.jar -jar project-SNAPSHOT.jar
Option 2:
The easier and much better solution is to use AppAssembler plugin. What it does it packages your jar in a directory structure that contains
dependent jars
the created jar
shell/windows scripts to execute it
have a look here http://www.mojohaus.org/appassembler/appassembler-maven-plugin/
Option 3:
If you do not want all the baggage and just wanted to have one jar-with-dependency
You may want to refer here How can I create an executable JAR with dependencies using Maven?
This will contain all the dependent jars within it.
Edit 1: For Option 1, Brad M mentioned that you can get a list of all your project's deps using the dependency plugin. dependency:build-classpath
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.classpathScope=runtime
You can find more examples here: 3 ways to run Java main from Maven.

Create install anywhere launcher from executable jar

I have a jar file that is being created by Spring Boot. Application runs smoothly when run by command java -jar. I want to create an install anywhere launcher with this jar file.
What I have tried is to send the Spring Boot main class (PropertiesLauncher). The issue is that calling it like this won't load the nested jars inside my executable jar and also the loader.path doesn't seems to work.
Is there a way to call the executable jar like java -jar from the install anywhere launcher?
I was thinking that another option was to create an install anywhere launcher for a script file and inside have the java -jar call. So another question will be:
How do I create an install anywhere launcher for a script file?
'execute command' step will do the trick:
Use this command line:
java -jar <path.to.jar.file>
Use EXECUTE_STDOUT, EXECUTE_STDERR and EXECUTE_EXITCODE built-in variables to catch errors and parse the jar's execution result.
Important notes:
You'll have to make sure your jar includes all of the dependencies (or at least set the classpath in the command line);
To include the dependencies within your jar using eclipse you can:
Export your project as a 'runnable jar file' and select the
'Extract/Package required libraries into generated JAR' option/s
Use Maven to build the project with dependencies; the
maven-assembly-plugin is required.
The 'execute command' will work for batch/cmd/shell scripts as well, but you'll have to make sure the scripts are extracted to a local folder such as %TEMP% or /tmp before you can use them.
Goodluck

How to run java program in maven

I have selenium maven project created. For running the tests I have to run one program that will create testNG.xml which contains list of test cases to be executed.
But for that I need to run one Java program which has dependencies on other Java programs.
You need to use the maven exec plugin.
Take a look at this http://www.mojohaus.org/exec-maven-plugin/usage.html
Check the dependency in pom.xml file.
Run command
mvn install
it will install all the dependency and create .class files.
To run java file
java -cp "define all the jar files path separated by ; or :" name of java file

Categories

Resources