Tuesday, September 29, 2015

Discovering transitive dependencies in maven

Sometimes we discover a dependency in the project that we didn't put there. The reason for this is often a transitive dependency(a dependency of our dependency).
Keeping an eye on transitive dependencies and tidying up the pom files time after time is a good practice.
A way of discovering transitive dependencies is to use the command "mvn dependency:analyse".

This command comes from a set of tools inside the maven-dependency-plugin.
In order to use it, you have to include the dependency plugin in your project like this:

 <!- If you are just using it in a single module project->  

  <!-- If you are using a multi module project then use this in the parent POM -->  

This plugin can do multiple things, the analyze command is very useful for finding more information about dependencies.

Unused declared dependencies found: The dependencies are not used in the project
Used undeclared dependencies found: Transitive dependencies used in the project(not declared in the pom)

The output of the command will be presented as warning in the terminal, if you want to be very strict about keeping your your pom tidy,
you could add the analyze command in the build lifecycle and configure maven to fail the build if any warnings are found(Maybe I explain that in another
post sometime ;) ).

Another great tool that comes in the dependency plugin and that its worth mentioning, is
 "mvn dependency:tree".
It will help you visualise the dependencies tree of your pom and understand it better.

Just some more interesting stuff about maven if you want to read more:

Thursday, September 24, 2015

First class functions Vs Higher order functions

First class functions means the ability to treat a function as a value, assigning it to a variable, or passing it as an argument to another function.

 //example 1 assignment  
   Function<String,String> postfixFunction = s -> s + "-postfix";  
 //example 2 passing as parameter  
   public void someMethod() {  
     stringManipulator(postfixFunction, asList("A","B"));  

A higher order function is a function that accepts other functions as arguments, or returns another function.

  //example 1 accepting function as arguments  
   public List<String> stringManipulator(Function<String, String> manipulator, List<String> data) {  
     return data.stream().map(manipulator).collect(toList());  
   //example 2 returning a function  
   public Function<String, String> postfixFunction() {  
     return s -> s + "-postfix";  

Friday, September 18, 2015

Don't be afraid of chinese restaurants ;)

How many times in your life have you shout to a non english speaking Chinese waitress at a restaurant in Soho, something like: "HEY!!! Why are you swapping the card 4 times?!?!?!!"
In this post I will prove that Chinese waitress are honest and often people panic, because they don't understand what Idempotency is.

The debit cards payment gateways are the classic example of idempotency.

In distributed systems terminology, we say that a service is Idempotent, when its clients can repeatedly send the same requests while the service produces the same result.
In other words, multiple identical requests are treaded by the service as a single request.

Important to note that this doesn't mean that the response is identical, it is just the service result that will be identical.
While the state of the response may change between requests, the result will always be the same, for identical requests.
In other words, it is save to call the service multiple times.

Let's have a look at a simple example of an idempotent function in java:

Receipt process(DebitCard debitCard, Amount amount) { throws NoPatienceException 
      if(not(transactionSentFor(debitCard,amount))) {  
           return cardProcessor.process(debitCard, amount);  
      else {           
           return blankReceiptFor(debitCard, amount);  

In the example above, we can clearly see that the conditional statement is safeguarding us from starting probably a distributed transaction across multiple parts of a distributed system. Instead of that, if we try to process again,  a blank/identical receipt with the returned

Come on, stop being affraid of chinese restaurants. 

Sunday, July 12, 2015

Why big corporations don't like retrospectives?

Not too long ago, I used to facilitate retrospectives for some of the teams in the department.
But for some strange reason their interest in retrospectives just disappeared.
I thought a lot about it, but I couldn't come up with an answer. Why?

Is it that they don't understand the advantages of retrospectives?
Is it that they have no time for retrospectives?
Or is it that they feel powerless when they do a retrospective?

In this brief text I want to just share my opinion on why I think corporations, have no interest in retrospectives(A bit of an speculative opinion, but just an opinion).

A retrospective is well known Agile practice that aims to improve any aspect of work. During a retrospective a team debates about things that matter for them and for the company: ideas, solutions, process, collaboration, communication, quality, recruitment, salaries, etc...
This are just some topics.

Unfortunately retrospectives are not being taken serious in many companies:
- in some, retrospectives are discouraged and seen as a waste of time
- in others shyly implemented within a constraint environment without visibility or power to
take any action. Just as a way of ventilating frustrations. 

So why is this that they are so unpopular?

The Answer is simple:
Companies fear change but specially, they fear decentralisation and lose of control.

The fact is that well organised regular retrospectives, could easily challenge the stablish organisational structure by making evident and visible its inefficiencies.

Many young technical companies quickly gain terrain to the prehistoric corporates not just because their decentralized and autonomous nature but also because of their eagerness and necessity to communicate and improve. Most of the successful ones often practice retrospectives.

The old school of building software companies has its days counted. What employees have to say about process inefficiencies, technical challenges, etc ... Is as important, if not more than the absolutely false illusion of rush that the corporation sees in the market.

Big corporations are organisations with huge amounts of resources and great potential for making significant impact in industry. Helping them getting rid of their fears is key for building not just good companies but a
healthy industry.

Software is continuous change, so embrace change, embrace retrospectives, embrace the future

Thursday, July 9, 2015

Maven build in colours

Did you ever wonder, how you can make your maven build display colours?
In this quick post I will show you what you need to do to show colours in your maven build.

  1. Navigate to wherever you have installed maven and go into the lib folder ~/apache-maven-3.3.1/lib
  2. Delete slf4j-simple-1.7.5.jar
  3. Find on the internet the following jars and add them to the lib folder

  4. Get out of the lib folder and navigate into the conf folder once in there create a file called log4j2.xml and add the following content:

    <?xml version="1.0" encoding="UTF-8" ?>
        <Property name="maven.logging.root.level">INFO</Property>
        <Console name="console" target="SYSTEM_OUT">
          <PatternLayout pattern="%highlight{[%p] %msg%n%throwable}" />
        <Root level="${sys:maven.logging.root.level}">
          <Appender-ref ref="console"/>
  5. Go to the terminal and try building a maven project

Monday, June 8, 2015

Stefan Birkner's system-rules library

Just a super quick post about an interesting library I just discovered :)

Its very rare the case when you will have to test things such as an String being printed in the console or a System property being set from the program... But in occasions it happens that.

Last week I discovered a library called system-rules, from Stefan Birkner. http://stefanbirkner.github.io/system-rules/
It is really interesting and easy to use, it can help you easily do tests that involve java.lang.System

Lets just quickly saw an example.
Imagine that for some reason you want to test that the console prints some message... I don't know you, but the only way I know to do that, is to redirect the stream that goes to the console, to something that you can control and read from(e.g a file, a log...). This kind of test would have lots of boiler plate. It would look somehow like this:

   public void consoleOutputOldSchoolTest() throws FileNotFoundException {  
     //Create some text file where the output will be redirected, so you can make an assertion later.  
     File testingFile = new File("testingFile.txt");  
     //Create a testing stream and give the file to it  
     PrintStream testingStream = new PrintStream(testingFile);  
     //Keep a copy of the old console output stream  
     PrintStream consoleStream = new PrintStream(System.out);  
     //Reset the object out to the new stream  
     //Write something to the "console"  
     //Rewire back to the original console output  
     //Just an informative message  
     System.out.println("all back to normal");  
     //Read the output that we are testing  
     String output = new Scanner(testingFile).nextLine();  
     //Do your assertion  
     assertThat(output, is("test"));  
     //Delete the test file  

With Stefan's library you can test the messages that go the terminal in the blink of an eye:

   public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();  
   public void systemRulesLibraryConsoleOutputTest() {  
     assertThat(systemOutRule.getLog(), is("test"));  

Ok, now back to work now ;)

Sunday, April 12, 2015

Install and configure Java in ubuntu easy

Whenever I change laptop or format my hard drive, I end up making a mess with the soft links to configure java...

This post is just a reminder too myself, for the future. I found a clean way I am comfortable with for setting my system to use java, via the update-alternatives tool that ubuntu has. This is how I do it:

1- Download the JDK from oracle and copy it to your user directory.
I like having it under ~/Java/jdk1.8.0_40

2- I often like installing maven, so I do the same, just I create another directory for it and I put in there the downloaded maven. ~/maven/apache-maven3.3.1

3- Then I set the necessary environmental variables for my user in the ~/.bashrc file.

export JAVA_HOME=/home/djordje/Java/jdk1.8.0_40
export M2_HOME=/home/djordje/maven/apache-maven-3.3.1
export M2=$M2_HOME/bin
export PATH=$PATH:$JAVA_HOME/bin:$M2  

4- I use update-alternatives to install in the system the java interpreter and the compiler.

sudo update-alternatives --install "/usr/bin/java" "java" "/home/djordje/Java/jdk1.8.0_40/bin/java" 1

sudo update-alternatives --install "/usr/bin/javac" "javac" "/home/djordje/Java/jdk1.8.0_40/bin/javac" 1

5- Again i use update-alternatives, but for this time to configure the default version of the interpreter and compiler to be used by default.

sudo update-alternatives --config java
sudo update-alternatives --config javac

When running the command, it shows all versions of java installed, to pick one

6- At the end I just run java -version to see that the system is using the version of java I want.

I like this way, it is easy to do and you can quickly switch to a different installed version by just using the --config option

Share with your friends