Bash Script to execute Java Project - java

I usually compile and execute from Java project using eclipse IDE. Now, I need to write a bash script to submit this task to a cluster. My project contains 3 packages and has been written as per the Maven architecture. Can anyone hint me as to how can I include the task of executing the java program in the my bash script?
I tried different things like first trying to execute the project using command line but it does not work. I already read the replies provided in this post: Compiling and running multiple packages using the command line in Java and some others, but didn't help.
Thank you for your help.

I strongly suggest you look into something like the Maven App Assembler plugin. I don't know about your cluster but the mentioned plugin can create run scripts for most major OSes and is highly configurable.

Related

Run Maven Project (with ant) in a single go using Remote Debug Configuration of IntelliJ

I have a Maven Project, Ant Project which are somehow coupled. If I have to make any change in maven project and test it I have to do following steps every time, which is very time consuming.
Steps
ant stopserver
mvn install
ant startserver
Access it on localhost:8080
And to debug anything I have to create a Remote Debug Configuration which connects to port 8000 and start ant server in debug mode.
All this is new to me as I have only worked on microservices based out of maven when there is a #SpringApplication class with main method which I could directly run/debug. In this project, there is no class with main method. It's a legacy spring mvc project.
I seriously want some way to do the same with current project. I tried going though the build.xml but ant steps are hardly taken less than a second.
Is there a way possible? Can it run like a normal maven project? Ant is probably building some db and ui parts (not very sure). But I only work on Java side.
Please help. Anything that could get rid of me having to run mvn install with every small change would also be helpful. To reduce the time taken by mvn install I used the script from this answer here and added that as well, as one of the before launch steps but the time taken is still the same.
I have tried following post but it did not work for me How to build maven project with ant script?
Open the lid and find out what exactly ant startserver does, and then create a launch configuration in your IDE that does the same thing. You might be able to cheat a bit and investigate the process in the operating system using its tools to get the invocation commandline.
Then run that launch configuration in the Debugger and tell your IDE to tell the JVM to hotswap newly compiled classes.
You should now have a much improved experience.
You may want to take the opportunity to teach Maven how to launch your server as that might enable the IDE to pick this up directly.
I was able to achieve at least one click start by Adding ant targets as part of the "Before Launch" inside "Run/Debug Configurations". To reduce the time taken by mvn install I used the script from this answer here and added that as well, as one of the before launch steps.
By enabling Logs, this also became user friendly.
Here is how my config looks like now,
However, I still have to do maven install.

Using Maven for Java program invocation

Background
I am a Maven newbie and I greatly like the fact that Maven knows where to pick up all the JAR files needed for executing a Java program (i.e. the fact that the required JARs need not be specified in the CLASSPATH environment variable since they are all stored in the .m2 repository).
I thought Maven could be the preferred way to invoke Java programs for real world applications but for a previous post of mine the following comment was received:
If you want to execute java programs I would suggest to create self running artifacts instead of using maven to execute a program
This brings me to the following questions:
Questions
Should using Maven as the tool for invoking Java programs be frowned
upon?
If yes (i.e. there are issues in using Maven as the
preferred method for Java program invocation), what are the better
alternatives and why?
Maven is a build tool and was not designed to run programs.
If you want to test your program, your IDE should be enough to start it without any classpath hassle.
If you want give the program to someone else, use the Maven assembly plugin or the Maven shade plugin to bundle the jars with the dependencies. Then you can run it on any computer with Java installed.

Robot framework +jenkins, testing java application

I'm working on CentOS 6.5, with Jenkins running. I've installed Robot framework plugin for jenkins, installed python 2.7 with Robot Framework (pybot works). I also wrote pretty easy program using NetBeans maven project with one unit test. My goal is to use RF in order to run said test.
At first i've tried to use Python version of RF, but eventually did it with jar stand-alone.
So my questions are:
1. Is it possible to add java libraries with keywords to Python version of RF or do i have to use jar stand-alone?
2. If i have to use .jar stand-alone, how can i run working test case on jenkins?
I've literally tried everything that i've found on this site (that's why eventually test-case worked), but i couldn't find anything else specificaly about what i'm asking.
You can use Jar edition with jenkins by using execute shell command
java -jar robotframework-3.0.jar -P TestedJarName.jar -d FolderForLogs RobotScript.txt
This will let you run test script. In script, you have to specify library with keywords - in other words, where in package is you .java file. For example, if you want to test Main.java that is in com.main package, you must write
Library com.main.main
Now, you can use all methods from Main.java
If you want just python script that runs maven tests, you just run it through shell pybot testname.txt
In text u need something like that Run mvn test and that's all.

Netbeans javac command

I am relatively new to Netbeans and Java, when I compile a project in Netbeans is there a way to view the equivalent javac commands, ie what I would have to run command line to produce the same result?
I use Netbeans 7.3.1 and you can see the building/compiling process in the output windows when you're building the program. Netbeans (my version) uses ANT to build program, so it produces a .jar.
If you want to know more about the details of how they compile a project, you can take a look at ANT http://ant.apache.org/, it's an useful build tool.
On your computer go to C:\Program Files\Java\jdk(the version you use)\jre\bin.
In bin you will find the commands you can use at the Command Prompt.
As David pointed, Netbeans uses Ant as its build tool, this means it uses a tool to parse a script that describes how to build each part of your project.
If you consider a simple Java program that is made of a single class this may not be significant and you could probably build it manually in the command line. However, any significant project (anything that does any interesting work) will need to compile many files, build the archive structure and every other task related to packing your application.
So, for a short answer you would have to consider the context:
1 - a simple class - yes you can compile manualy reading the output from the console
2 - for a simple project (that means, many classes) - yes, but starts to gets complicated
3 - for a real project (that means, many classes and resources) - no, it is not viable (and this means reasonable and confortable) to do it manualy.
For more info on compiling Java programs (which I think is your real interest) you should check the Javac page for a start (find it here).

Java on demand build system

I have a vague memory that I've seen a build system for Java or some other JVM lang which you could run in continous mode (so to speak). This would make the build system run in the background, watching the source dir for changes. As soon as a source file changes, the build system kicks in and re-runs relevant parts of the build process to produce a fresh artifact.
Ring any bells?
Thanks
Is it the gradle daemon?
This keeps gradle running in the background because it has long start up times - and scans the file system to identify when files change.
There are lots of engines that support continual checkouts/building/testing (e.g. Jenkins nee Hudson, CruiseControl, TeamCity etc.).
If you're looking at JVM languages other than Java, then (for example) the Maven Scala plugin supports continuous building and testing upon file changes. e.g.
$ mvn scala:cctest
will run the compile/test cycle in a loop watching for source changes and acting upon them.
If your build tool or language doesn't support this natively, it's trivial to write a shell script to watch a directory structure and execute another script/tool upon change. See this SO question for solutions.
I figured out that it was in fact sbt that I meant. There is another question here on SO relating to how to build Java projects: Use SBT to Build Pure Java Project. Thanks everyone for your help.

Categories