Linux, Java, Git and Maven…

Here, I will explain how to *simply* get a Java project with Maven compile instructions (pom.xml) on Github to your system, and compile it using Maven. This requires a few things. We’ll use Potato. You can fork this project with your own Github account, and then you can say that you’ve “Forked a Potato” (Seriously, it’s just a fun project with limited to no practicle use) We’ll be using Potato as our reference example project. It is Java, with Maven, on Github and will compile under Linux. https://github.com/AeSix/Potato

First, let me state that I use Ubuntu GNU/Linux distros. Life is too short to worry about getting everything installed “the hard way” – Ubuntu’s apt-get is, in my humble opinion, the simplest way to do routine software installs. I’ve built Slackware linux, ran through Fedora Core, messed with Gentoo and Suse, and various other distros, including Debian (Ubuntu’s papa distro) For everything I’ve ever needed, Ubuntu has provided much simpler than other distros. That may not be the case for everyone, however. So with that all said, I’ll be talking about “Linux” pertaining to Ubuntu GNU/Linux specifically. These instructions may be translatable to other Linus distros, to UNIX distros (FreeBSD, MacOS, etc) or even for Windows with varrying degrees of success. I won’t discuss these here though.

The first step, of course, is to procure suitable environment and install your OS. We’ll assume this is done. If it’s not, you’ll want to do that, after reading the rest of this. You’ll find specific instructions for your environment online, and so I won’t waste time here detailing that.

Next, you’ll need to install Java. This is a considerably more difficult process on any Linux distro, if you’re wanting to use Oracle Java (as opposed to OpenJDK), that’s as simple as
> apt-get install open-jdk
I prefer to use Oracle JDK, however that’s a personal preference. You can find installation instructions if you were to search for “Oracle Java Ubuntu webupd8” – there’s plenty of info on that and does not need to be duplicated here.

Once you have Java installed, you’ll want to get a Git client and Maven. Again, there are installation instructions online for each of these. I suggest Gitlab’s instructions, but DigitalOcean has some nice write-ups as well.

At this point, we’ll assume you can do these things:
login to your Linux user account, and perform commands with sudo. I highly suggest NOT using root user!
– Test your network connection:
> ping google.com
– Test your Java installation:
> java -version
– Test git client:
> git –version
– Test Maven installation:
> mvn –version

Assuming this all went to plan, you should now create your work environment. I suggest making a ‘dev’ directory under your /home/username, with a git directory under that. OCD organization!
> cd ~ && mkdir dev && cd dev && mkdir git && cd git && pwd
Provided this worked, you should see something akin to, with “yourusername” being replaced:
/home/yourusername/dev/git

Now all the setup is done, let’s clone us some code!
> git clone https://github.com/AeSix/Potato
> cd Potato && ls
The first command here will reach out to Github and fetch a copy of Potato from github.com/AeSix/ and create a new directory under git/ called “Potato” The git client will always create a new directory in your cwd with the project name as the new directory name. The second command simply puts us into that directory and lists it’s contents. Verify that the contents on your system match that of the repository you cloned from. If it does match, Congrats! You’ve cloned a Java Maven Git project!

Let’s build this project. This is what I *LOVE* about Maven, how simple it is! You’ll be addicted and wanting to compile github projects every day! (Ok, maybe I alone got a bit overly enthusiastic about Maven when I first got it working!)
> mvn clean install
That’s it! That’s all you needed to do! Of course, you *have* to be in the directory with the project’s files for this to work, and the project *has* to have a ‘pom.xml’ file. Of course, the project also has to be fully written, not broken, and compatible with your version of Java and potentially your OS (Very old OSes may not have some functions that newer Java projects require, but then your Java version would be subject to these and you’d never get a new enough Java installed to even build with much less run the project with)

Oh! You actually want to run the project? I guess I can tell you where the compiled jar is.
You should still be in /home/yourusername/dev/git/Potato – so do
> pwd
and confirm you are. If you’re not, then something went awefully awry and you should figure that out. Let’s assume there’s no problems though. Now do
> ls
You should now see a “target” directory. Again, assuming all things went well, do
> cd target && ls
And you’ll see “classes” and “Potato.jar” So let’s run Potato!
> java -jar Potato.jar
You should be greeted with a friendly, if passive-aggressive, yet humerous message from your new Potato.

If *anything* went wrong, I suggest starting from the top of the first checklist, pinging Google. If you still can’t get it going, you may need to do some more research.

Here’s the TL;DR for those who just need a reminding of how easy (so easy, it’s forgettable) Java/Maven/Git is:

Get project url:
example: https://github.com/AeSix/Potato
Enter your working environment:
> cd ~/dev/git
Clone project to local system:
> git clone https://github.com/AeSix/Potato
Build with Maven:
> mvn clean install
Test the built jar:
> java -jar Potato/target/Potato.jar
Rejoice at the deliciously prepared Potato!

To update the local project files:
> cd ~/dev/git/Potato
> git pull
The git pull command has to be done from within the project’s local directory, otherwise it wouldn’t know which project to update.

Leave a Reply

Your email address will not be published. Required fields are marked *