Shaun Abram
Technology and Leadership Blog
Intellij Maven folders
I haven’t been coding a lot recently, and when I spun up a new maven spring boot project and imported in to IntelliJ, I got confused about which folders are supposed to be marked as source (src, src/main or src/main/java?) and test (src/test or src/test/java?).
Dev Environment Setup
This post covers some of the basic steps I need to do each time to setup my dev environment on a new mac. I use this post in conjunction with the mac tools post.
Tags: devenvironment, mac, macbook, productivity, setup, tools
Kubernetes Admin GUI
docker for mac now comes with Kubernetes support built in to it. It is now the easiest way to experiment with Kubernetes locally (previously, minikube seemed to be the easiest way). This feature was announced at DockerCon Europe in late 2017 and is supported in docker for mac versions 17.12.0-ce-mac45 or later. Note however, you do need to use the Edge version.
When you get Kubernetes running via docker for mac, you can access the admin GUI as follows…
Tags: k8s, kube, kubernetes
Using Docker with a maven project
If you have a maven project, there are a plethora of ways to enable it to run within a docker container.
Tags: containers, docker, helloworld, Maven, mvn, springboot
Maven (mvn) build hanging
I recently had a problem with a maven build hanging when doing a ‘mvn install’.
I found some pointers in this post, and posting here in a modified form: http://forum.spring.io/forum/spring-projects/roo/121725-what-diagnostic-action-to-take-when-maven-hangs
1. In the hanging shell, hit CTRL-Z – this will suspend the program and give you a pid. An alternative way to get the pid is to do a “ps -ef” and search for you mvn command e.g. “clean install”
2. type bg [ENTER] – this will send the program to the background
3. Do a kill -3 of the process – it will dump a large amount of text – make sure your command line terminal history window is long enough to capture it all – will be several thousand lines of text
(alternative to #3 – you can try jstack with the pid, it is a Java stack trace generator)
4. You can see what threads are waiting on other threads from there (maybe, sometimes it’s a tight CPU loop)
URL Encoding
URL encoding, also known as percent encoding, takes certain “reserved” characters and standardizes (or canonicalizes) them.
Why? This is normally done when transmitting data in html forms. For example, the # character has a special purpose in html (as an html anchor) and so is converted to make it clear that it is part of that data, not part of the html document where it is to be displayed.
Another (related) use is for prevention of XSS attacks. If your web page allows a user to enter text (for example, a comments box on a blog), it would be very easy for a motivated user to enter malicious text that will be interpreted as a script.
Tags: helloworld, springboot, urldecoding, urlencoding, webdev
Running an individual unit test with maven
Individual tests can be run from maven. This must be done from within the folder that contains the source (as in the folder that contains src/test/java), which will likely be either be the root of your maven project, or in one of your modules.
For surefire (unit tests), to run a whole test class, this will be something like:
mvn -Dtest=com.your.package.YourTest test
or, to run a specific test method:
mvn -Dtest=com.your.package.YourTest#testMethod test
For failsafe (integration tests), it will be something like:
mvn -Dit.test=com.your.package.YourIT verify
This is covered in the maven surefire plugin docs and maven failesafe plugin docs respectively, but those examples don’t mention packages names, which are required.
Tags: failsafe, integrationtesting, JUnit, Maven, mvn, surefire, Testing, unittesting
Java vs .Net Accessor Modifiers
I’ve been looking at some .Net code recently, and I wanted to do some comparisons on Java access modifiers vs .Net to help me better understand the code.
Tags: access, accessmodifiers, dotnet, modifiers
Microservices
A microservice is a small, focused piece of software that can be developed, deployed and upgraded independently. Commonly, it exposes functionality via a synchronous protocol such as HTTP/REST.
That is my understanding of microservices, at least. There is no hard definition of what they are, but they currently seem to be the cool kid on the block, attracting increasing attention and becoming a mainstream approach to avoiding the problems often associated with monolithic architectures. Like any architectural solution, they are not without their downsides too, such as increased deployment and monitoring complexity. This post will have a look at some of the common characteristics of microservices and contrast them with monolithic architectures.
Tags: microservices, REST, SOA
Web frameworks
There is no right answer to the question of what is the best Java web framework. Still I end up asking it to myself every time a new project crops up. I did a post on a related talk I saw at JavaOne last year, which provoked a lot of debate and some really interesting responses. More recently, this report from Zero Turnaround is useful and this comparison from Matt Raible is also well written.
I have also been swayed in the past by the Thoughtworks technology radar in which component based frameworks (which, I think, in the Java world includes JSF, Wicket and Tapestry) get a thumbs down e.g. see the May 2013 radar. GWT has also in the past (see July 2011 radar) been singled out as something to avoid. Presumably Vaadin falls in to the same ‘hold’ category. Full disclosure, I’ve had limited exposure to these types of frameworks personally though.
My own preference remains Spring MVC. It is relatively easy to setup (especially with Spring Boot), provides decent testing support, and obviously integrates well with the rest of the Spring ecosystem. I am admittedly biased due to already knowing Spring core, but so be it.
My recent, albeit limited, experience with Struts2 is that I have been fairly pleasantly surprised. It wasn’t as bad I was expecting! The Action classes, which are instantiated for each request, and hence threadsafe, are fairly easy to use and test. I am not so fond of the xml mappings and the variable passing that gets done there though. It seems kind of clunky, although there may be a better way I am not aware off.
Still, I am not likely to start using Struts by choice on my own projects anytime soon. Spring MVC remains my go-to web framework.
Tags: springmvc, struts, struts2, webframework
Maven Quickweb Archetype
The maven Quickweb archetype allow you to create a new project with a layout that is essentially a combination of what you get with the standard maven archetypes of quickstart and webapp. The readme on Github contains more details: https://github.com/sabram/maven-archetype-quickweb
If you are interested in the underlying workings, the ‘recipe’ for the archetype is the archetype descriptor, archetype.xml, which is located in the src/main/resources/META-INF/maven/ directory. It specifies what files the generated project will be made up of, in addition to the prototype pom, all of which are located in the archetype-resources folder.
Links:
- Apache source code for maven-archetype-quickstart and maven-archetype-webapp
- Apache guide to creating archetypes; Wiki on creating an archetype
- How is metadata about an archetype stored?
- Stackoverflow question on how to create the src/main/resources folder with the quickstart folder. I posted a link to my maven-archetype-quickweb archetype there.
- Stackoverflow question on how to use a custom maven archetype
Tags: archetype, Maven, mvn, quickweb
Live Templates in IntelliJ
As described here, IntelliJ’s live templates let you easily insert predefined code fragments into your source code.
I have posted some of my most used templates below, a link to my complete list of template files on GitHub (as a reference for myself when I setup new IntelliJ environments) and the steps I took to add the IntelliJ settings file to GitHub.
(more…)
Tags: git, github, ide, intellij, productivity, shortcuts, tools
Testing for expected exceptions in JUnit
Unit tests are used to verify that a piece of code operates as the developer expects it to. Sometimes, that means checking that the code throws expected exceptions too. JUnit is the standard for unit testing in Java and provides several mechanisms for verifying exceptions were thrown. This article explores the options and their relative merits.
(Note: There are other good options for testing for exceptions using AssertJ, rather than vanilla JUnit. See https://www.baeldung.com/assertj-exception-assertion)
(more…)
Tags: exceptions, JUnit, tdd, unittesting
Java8
Java8 isn’t scheduled for for release until March 2014, but early release versions have been available for a while.
Some of the most interesting new features are:
- Streams
- Functional interfaces
- Default methods
- Lambdas
- Java Time
Java7 – A look back
I started writing a blog post on what’s new in the upcoming Java8 release, and thought I would start by doing a quick look back at what Java7 brought to us.
Java7 was released back in July 2011, and was described as “more evolutionary than revolutionary”.
“There are some significant improvements, but no really earth-shattering or ground-breaking kinds of features.” – Oracle chief Java architect Mark Reinhold
It didn’t contain the much hyped lambda expressions for example. Still, it did bring a lot of other neat additions to the Java language. You can see the more at http://docs.oracle.com/javase/7/docs/, or read on for my summary.