How do I package properly with JRuby? - java

I'm coding a project that uses JRuby and I want to be able to do three things in one command, right off the bat after cloning the repository.
Install rubygem dependencies.
Install maven dependencies.
Package everything into a standalone jar, including JRuby.
What is the simplest and most straightforward way to achieve this? I could probably hack around with custom build commands in my pom.xml, but it seems like other people must have wanted to do this before. I don't want to mess around with a shell script that downloads stuff through maven and then shoves my ruby code into ruby-complete.jar, after "java -jar ruby-complete.jar -S gem install"ing my gems. I want a simple, declarative file that says: "these are the dependencies, now parse me and figure out how to get what cheshircat wants."
Is there a way to do this with maven? I've tried jbundler, but it keeps giving me errors, and I've tried it on multiple platforms.

I would look at the torquebox jruby-maven-plugins. I use them for my work to do two of the three requirements you list:
'Install rubygem dependencies' using the gem-maven-plugin
'Install maven plugins' like any other plugin because it's maven and maven is driving the build and packaging.
The gems get installed to your project's ./target/rubygems directory.
I am sure you can jar up whatever you want with the maven-jar-plugin.
I wrote up a more detailed explanation:
http://thechurchofautomation.com/java-ruby-interoperability-torquebox-maven-plugins/

Not sure about the maven dependencies, but in general warbler is the tool you want for packaging up everything, including jruby and gem dependencies, into a standalone jar.
https://github.com/jruby/warbler
It will do .war as well as .jar. If your thing doesn't look like a webapp (doesn't have a rack.up file), it'll do a jar though, just what you want.
It doesn't need any extra declarative config, it just uses what you've already got, like your Gemfile.
You may have to do maven as an extra step, but you could try asking the warbler folks for advice.

Related

How can I package a maven jar and output multiple copies of the resulting JAR file to different folders?

Pretty much what the title says. I'm building Minecraft Spigot plugins for servers running under BungeeCord and running the mvn package plugin in IntelliJ results in the generated JAR file being located in the project's "target" folder. Instead, I need to output multiple copies of the generated JAR into multiple "plugin" folders for various servers. I'm not sure how or if this is possible to do with Maven, but I would like to know if there is a way to do that in pom.xml. Having to copy the JAR every time I build it slows down the development process. Any help would be greatly appreciated!
You should be able achieve the same thing with symlinks (symbolic link). It allows the file system a way to have a reference to another file location without actually making an explicit copy (kind of like a shortcut). This is also more in the spirit of maven's philosophy that each project should build only one artifact.
How you make symlinks depends on your operating system. You would need to make a symlink for each separate location that you need to "copy" to.
Mac / Linux:
ln -s /project/target/my.jar /project/server/plugin1
Windows:
mklink /project/target/my.jar /project/server/plugin1
Other info about symlinks (e.g. if you need to delete):
https://www.howtogeek.com/howto/16226/complete-guide-to-symbolic-links-symlinks-on-windows-or-linux/
https://www.howtogeek.com/297721/how-to-create-and-use-symbolic-links-aka-symlinks-on-a-mac/
Building multiple jars is fairly tricky in maven. See this post for some additional details - it suggests using a maven ant plugin if this is what you really want, but I would recommend against this approach. Symlinks should be easier to work with.
Ant to Maven - multiple build targets

How do I check installed JARs, external libraries, etc. on three different Java IDEs?

I've written programs in several languages and have tutored students in computer science, but just starting to learn Java on my MacBook. Regarding this question, I'd be happy with any answer that points me to available information or tutorials that address my question; I'm capable of understanding advanced things.
I've been searching for the right IDE for me as well as something I can use with my students, and I've tried IntelliJ, Eclipse, and VS Code. Along the way I've installed external JARs to provide extra capabilities, such as Apache Commons.
Things are getting confusing. I've lost track of how I got to the present state in each IDE. I'd like to understand better how to know the overall Java environment that any given project is using on each of these IDEs, including any external JARs and where they are located. And I'd like to know if they borrow from the Java system environment.
My goal is to understand how my own system got to the way its currently configured, to update my configuration on a project-by-project basis, and to help my students get a matching configuration.
I'd also like advice on the right way, or simplest/cleanest way, to install external JARs.
Maven
Question: I'd also like advice on the right way, or simplest/cleanest way, to install external JARs.
If you really wanna work in a organised way and wanna focus completely on coding rather than looking for dependencies to work with , then try building your projects with Apache Maven. The magic wand of Maven projects are pom.xml file where all magic happens depending upon your wish.
Maven is a build automation tool used primarily for Java projects. Maven addresses two aspects of building software:
Describes and manages how software is built.
Describes and manages dependencies (various libraries used by your code).
Why Maven:
De facto standard
Able to compile, test, pack and distribute source code ( different Goals)
Robust dependency management (Most important from my point of view)
Extensible via plugin
Good community support and many fan boys around.
The big 3 IDEs (IntelliJ, NetBeans, and Eclipse) all having good
support for Maven, letting you use Maven as a substitute for their
own proprietary project definition and build process.
Maven famously caches all of its dependencies in the ~/.m2
directory, which is sometimes called the local Maven repository.
Maven local repository keeps your project's all dependencies (library jars,
plugin jars etc.). When you run a Maven build, then Maven automatically
downloads all the dependency jars into the local repository. It helps to
avoid references to dependencies stored on remote machine every time a
project is build.
You can simply deploy your project as JAR, WAR, or EAR file and use it on different IDEs or as standalone.
All IDEs need a way to know your project's dependencies. You can either tell them that yourself or let a build tool do that.
Manual dependency handling: by adding the jars to your project. This is probably the fastest way when working on a small project, with one developer, on a specific IDE, with few dependencies. Usually when telling the IDE that this .jar is a dependency of your project, the IDE stores that reference to a project-specific file (eg. in Eclipse the .classpath file which you can edit with a txt editor and see the dependencies yourself). However, it kind of locks your application to your IDE. Most IDEs have cross-IDE support for import and migration, but using both IDEs at the same time can be confusing when a dependency is added to one and has to be repetitively added to other as well. Furthermore, your dependencies have dependencies on their own. By adding manually your jars you are responsible to find and download their own dependencies as well.
Use a build tool: There are 3 standard such tools right now: Apache Ant with Ivy, Apache Maven and Gradle. All of them have support in the major IDEs for Java: IntelliJ IDEA, Eclipse and NetBeans. All of them use some extra build-tool specific files to store your project's configuration and subsequently configure your IDE and the IDE-specific files. That way, your project becomes IDE-agnostic, the IDE outsources the dependency handling to the build tool. These tools will download any direct or transitive dependencies of your project in a local directory or you can compile jars in a specified folder. From those, Ant is the oldest (with Ivy adding dependency handling support), Maven was developed after that and Gradle is the newest and probably the most flexible. In production however Maven is by far the most established one right now.
It would be also useful to look up the Standard Directory Layout. If you adhere to that, it will be easier to work/start with either Maven or Gradle.
Finally, you can search and find most of the free libraries in Maven-Central where conveniently their Ivy/Maven/Gradle script is added as well for you to use on your build-tool script. In many cases a .jar is provided as well if you prefer to manually add it as a dependency.
Regarding VS Code, I think it supports these tools through plugins but I'm not sure.

What is the advantage to build an executable JAR

Today at work I came across something interesting. Say i have an old java project that were compiled with an ant build file and we have converted this project into a maven project. So now to build this project, we only need to do a mvn install.
When i do call
mvn install
I get a myproject.jar under the target folder, along with all the dependencies under a lib folder inside the target folder.
To run the executable of this jar I need to do something like :
java -classpath $classpath com.myproject.Mainclass $myArgs
Where $classpath is the path to all of my external libs and where $myArgs is the arguments that is passed to the main function.
I came across this website and I'm really considering to use the spring boot maven plugin to package my executable jar.
Wouldn't it be easier to execute it if all the dependencies are packaged in a single jar file ?
Why would I use the manual configuration vs the Spring Boot Maven Plugin for the executable jar ?
What are the pro and the cons of doing this ?
As the article you had linked covers with pros and cons how to do such single jar file packaging, I'll write out things that you need to consider if you want to use this approach.
Pros:
1. Simplicity of deployment
Users don't have to maintain any dependencies. All they need to do to run the app is get the jar file and execute java -jar file.jar.
2. No easy way to update dependencies by user
If your app uses some external dependencies, you can be sure they are in version that you have chosen. Using "classic" approach user can easily update it by himself to the version that may require some migration steps in your app.
Cons:
1. Size of final package
If your app has large dependencies, every update will require users to download the whole package,
even if dependencies haven't changed.
2. No easy way to update dependencies by user
To change a version of any dependency you will need to update the whole package, where using the old way you could update only the dependency jar.
Summarizing, if your app doesn't have any heavy (in sense of file size) dependencies, I'll personally use single jar file approach. Even if your dependencies changes frequently. It's a lot easier to change a single file, no matter if your app has to be updated or some of its dependency.

How to build a project using libvirt java binding

it might be a stupid question but how do I set up an environment to develop a test application using libvirt?
do I have to set up ant or maven project or can I just copy java files to my src folder in eclipse ?
Thanks
If you use Eclipse, you can build your classes and run your program in Eclipse without using Ant or Maven. In fact, Eclipse is usually completely ignorant of your Ant build.xml or your Maven pom.xml file. Eclipse uses its own build technology (although you can ask it to run your pom.xml or build.xml). However, a pure Eclipse way of building a project means there is no way to build and run your project except with Eclipse. If you have a continuous integration system, or someone downloads your project and simply wants to build your jar or war file, there's no way they can do it.
Actually, not entirely true. You could write a shell script to compile your code via the javac commands and jar it up via the jar command. What Maven and Ant do is give you a framework to help build your application and remove system dependencies.
Ant has an advantage of flexibility. You can do things easily in Ant that are harder to do in Maven. The disadvantage is that Ant has the flexibility to do things in a really, really bad way. I almost always recommend for developers to use Maven for new projects. It forces them to write their project in a standard way, and eliminates the need to write Ant build scripts which most developers really can't do.
What if you don't know Maven, but know Ant? I still recommend that you use Maven and take this as an opportunity to learn Maven.

Packaging and deploying a Jython program from Eclipse

So I've been pigeon-holed into writing some Jython code. I've been using the latest version of Eclipse IDE with the PyDev plugin for development. Up until now, things have been moderately tolerable. I've gotten all my Python scripts working and I'm successfully including a couple of JAR files and the class directory of another Java project as external dependencies. Everything seems to run fine through the Eclipse IDE.
Now I need to package everything up and deploy it. From what I can gather, the best way to do this would be to package everything up in a JAR file. The Jython documentation suggests starting out with the jython.jar file and adding to it. OK. So I modify my main python module and start adding all my python source to the JAR.
It executes but of course can't find all the external dependencies.
How is one supposed to add the external JAR files so that they are correctly seen by the Jython interpreter? How is one supposed to manage more complex dependencies in a setup like this?
Is there a plugin for Eclipse or maybe something like Ant or Maven that can handle all of these steps for me with the push of a button?
I can't be the first person that has needed to deploy Jython code with complex dependencies can I?
I've made some headway on getting this all working so I thought I would put some notes here in case they help anyone else out. I'd still like to hear from others on their experiences trying to put together something like this.
It turns out that Eclipse as of 3.5 has a project export option for Java -> Runnable JAR File. If you use this option, you can point to a Java main class in the export wizard. You also have the option to have it repackage all the JARs that you are dependent on in your new JAR file. Make sure to check the box to save the export as an ANT build so that you can repeat the process quickly. NOTE that the first time you do this through the interface, it may fail, but it will still have created a JAR file.
Now here's where it gets strange. To track all the dependencies, I am still using a mostly incomplete Maven build in my project. I create the Maven .POM file. And I told Maven what my external JAR dependency was. I then told Maven to do a dependency update for me. It pulled everything into my Maven repository as expected.
Now when I do my ANT build, it appears that it is getting its list of JARs to include in the final build from Maven. I'm not really sure if it is supposed to work that way. I'm also not 100% sure that it is working. I guess I'll find out when I have to add another external JAR to the project.
Anyways, if you follow this question you'll see that you can take the latest builds of Jython and pull the org.python.util.JarRunner.java file out and use it in your own project. This is you Java.main class that you will need to point your ANT build at. From there, convert your main Python/Jython script to be the run script that was talked about in that question.
Next, build up another copy of the Jython JAR file in your Jython directory. This one should have the /Lib directory pulled into the JAR. Save that off and then point your Eclipse IDE Jave Build option for your PyDev project at that JAR as an external dependency. Your JarRunner will now work and execute the run.py file under Jython.
If all that works, you should then be able to rerun the ANT exported build from earlier and you will end up with a single JAR file that you can execute on the command line as:
java -jar {yourjar} args
And distribute to your customers without any additional dependencies.
If that all seems a little bit convoluted, it is. If anyone has a better way of setting this all up using Eclipse, please let me know.
Make your life easier and just use Maven and the mavenjython compile plugin.
See my answer of this question: Using Jython with Maven
You get full automation of the build and deploy process, and the result is a jar that includes jython and all other dependencies.

Categories