Finding project in workspace with specific maven dependency

1. Context

You have several projects in your workspace and you were wondering which projects are depending on a specific version of a maven dependency.

This article will show you how to solve this from the command line.

2. Generating Maven Tree

To print the dependency tree of a Maven pom, you can run the following command:

1
$ mvn dependency:tree

Output for e.g. javaslang submodule would be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
robbypelssers1@Macbook-Robby-Pelssers:~/Documents/pelssers/javaslang/javaslang$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Javaslang 3.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ javaslang ---
[INFO] io.javaslang:javaslang:jar:3.0.0-SNAPSHOT
[INFO] +- io.javaslang:javaslang-match:jar:3.0.0-SNAPSHOT:compile
[INFO] +- junit:junit:jar:4.12:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] \- org.assertj:assertj-core:jar:3.3.0:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.745s
[INFO] Finished at: Mon Jul 18 20:10:16 CEST 2016
[INFO] Final Memory: 17M/309M
[INFO] ------------------------------------------------------------------------

3. Filtering on the line with specific maven dependency for e.g. hamcrest-core version 1.3

1
2
robbypelssers1@Macbook-Robby-Pelssers:~/Documents/pelssers/javaslang/javaslang$ mvn dependency:tree | grep org.hamcrest:hamcrest-core:jar:1.3
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test

4. Finding all pom files in workspace or specific project

1
2
3
4
5
6
7
8
9
robbypelssers1@Macbook-Robby-Pelssers:~/Documents/pelssers/javaslang$ find . -name pom.xml
./javaslang/pom.xml
./javaslang-benchmark/pom.xml
./javaslang-gwt/example/pom.xml
./javaslang-gwt/pom.xml
./javaslang-match/pom.xml
./javaslang-pure/pom.xml
./javaslang-test/pom.xml
./pom.xml

5. Pipe the search results (pom files) as input for generating dependeny tree

Ideally we want to pipe all pom’s as input for generating the maven dependency tree and next filter on the specific dependency.

The problem is you will ony output something like

1
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test

So we still don’t now what file lead to this output printed to the shell.

We can however use following little trick. We basically first find all pom files, next we first make sure to print which one we are processing. Next we execute inside the folder of the respective pom the maven dependency tree command.

Now we need to make sure we both grep for lines containing ‘Processing’ AND also lines containing our maven dependency.

1
javaslang$ find . -name pom.xml -exec echo 'Processing {}' \; -execdir mvn -f {} dependency:tree \; | grep "org.hamcrest:hamcrest-core:jar:1.3\|Processing"

This will result in below output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Processing ./javaslang/pom.xml
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
Processing ./javaslang-benchmark/pom.xml
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
Processing ./javaslang-gwt/example/pom.xml
Processing ./javaslang-gwt/pom.xml
Processing ./javaslang-match/pom.xml
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
Processing ./javaslang-pure/pom.xml
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
Processing ./javaslang-test/pom.xml
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
Processing ./pom.xml
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
Share Comments

How to use different SSH keys for different Github accounts

Github image

1. Context

As self-employed developer I have two github accounts:

So I would like to use a different ssh key to authenticate with per account.

The default ssh key is coupled to robbypelssers and I created a second ssh key for pelssersconsultancy.

1
2
3
robbypelssers1@Macbook-Robby-Pelssers:~/.ssh$ ls -la *pelssersconsultancy*
-rw------- 1 robbypelssers1 staff 3243 Apr 23 11:39 id_pelssersconsultancy_rsa
-rw-r--r-- 1 robbypelssers1 staff 757 Apr 23 11:39 id_pelssersconsultancy_rsa.pub

In order to always couple the my private .ssh key ‘id_pelssersconsultancy_rsa’ to
any repository from pelssersconsultancy I added the following entry to .ssh/config

1
2
3
4
5
robbypelssers1@Macbook-Robby-Pelssers:~/.ssh$ more config
Host github.com-pelssersconsultancy
HostName github.com
User git
IdentityFile ~/.ssh/id_pelssersconsultancy_rsa

So in order to checkout the project mytodolist normally I would do

1
$ git clone git@github.com:pelssersconsultancy/mytodolist.git

but instead now I can do

1
$ git clone git@github.com-pelssersconsultancy:pelssersconsultancy/mytodolist.git

Just to verify:

1
2
3
4
$ git remote show origin
remote origin
Fetch URL: git@github.com-pelssersconsultancy:pelssersconsultancy/mytodolist.git
Push URL: git@github.com-pelssersconsultancy:pelssersconsultancy/mytodolist.git
Share Comments

Using MapReduce with Morphia and MongoDB

Map Reduce MongoDB

1. Precondition

You have a database called HELLOWORLD with 1 collection called Person containing following documents:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[
{
"firstName": "John",
"lastName": "Doe",
"age": 35,
"location": {
"country": "NL",
"city": "Maastricht"
}
},
{
"firstName": "Mark",
"lastName": "Doe",
"age": 45,
"location": {
"country": "NL",
"city": "Eindhoven"
}
},
{
"firstName": "Pieter",
"lastName": "Nelissen",
"age": 55,
"location": {
"country": "NL",
"city": "Amsterdam"
}
},
{
"firstName": "Edgard",
"lastName": "Lejeune",
"age": 25,
"location": {
"country": "FR",
"city": "Paris"
}
}
]

2. Find number of persons per city for country NL

1
2
3
4
5
6
7
8
9
10
11
12
13
robbypelssers1@macbookpelssers:~$ mongo
MongoDB shell version: 3.2.3
connecting to: test
> use HELLOWORLD
switched to db HELLOWORLD
>
var mapFunc = function() { emit(this.location.city, this.age); };
var reduceFunc = function(k, v) { return Array.sum(v); };
db.Person.mapReduce(mapFunc,reduceFunc, {out: { inline: 1 }, query: {"location.country": "NL"}})

3. Output from shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"results" : [
{
"_id" : "Amsterdam",
"value" : 55
},
{
"_id" : "Eindhoven",
"value" : 45
},
{
"_id" : "Maastricht",
"value" : 35
}
],
"timeMillis" : 13,
"counts" : {
"input" : 3,
"emit" : 3,
"reduce" : 0,
"output" : 3
},
"ok" : 1
}

4. How to do it in Java using Morphia and MongoDB Java driver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Datastore datastore = ...
DBCollection collection = datastore.getCollection(Person.class);
DBObject query = new BasicDBObject("location.country", "NL");
String mapFunc = "function() { emit(this.location.city, this.age); }";
String reduceFunc = "function(k, v) { return Array.sum(v); }";
MapReduceCommand mapReduceCommand = new MapReduceCommand(collection, mapFunc, reduceFunc, null,
MapReduceCommand.OutputType.INLINE, query);
MapReduceOutput output = collection.mapReduce(mapReduceCommand);
output.results().forEach(result -> {
DBObject key = (DBObject) result.get("_id");
Double value = (Double) result.get("value");
//do something with this data....
});
Share Comments

How fast are your collections?

JRebel Collections cheat sheet

collections cheat sheet
Share Comments

How to setup your own blog with Github Pages and Hexo

1. Create a new github account

Navigate to Github and click the Signup button. It is important to choose the same
username as the one you want to use for [username].github.io

Let’s assume you created a github account with username [johndoe] and email address [johndoe@gmail.com].

2. Create a new repository

Github pages explains how to setup your own blog site, so you can check it for reference.
Log in on Github and click on the button ‘New Repository’.

Now you should see a similar page like below with Owner [johndoe]. Fill in [johndoe.github.io] as repository name.

Creating github repository

3. Setup you ssh key for github

Github is a versioning system where each different version of any file belonging to your site will be saves and tracked.

However, in order to be able to save your files from your computer to github, you need to do following steps:

  • generate an ssh-key for the above created github account

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    johndoe@laptop:~$ ssh-keygen -t rsa -b 4096 -C "johndoe@gmail.com"
    Generating public/private rsa key pair.
    Enter file in which to save the key (/Users/johndoe/.ssh/id_rsa): /Users/johndoe/.ssh/id_johndoe_rsa
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /Users/johndoe/.ssh/id_johndoe_rsa.
    Your public key has been saved in /Users/johndoe/.ssh/id_johndoe_rsa.pub.
    The key fingerprint is:
    SHA256:zFNrmBSufsH/URijFp/+rO9iKlRmj8XRjB1HasidLDE johndoe@gmail.com
    The key's randomart image is:
    +---[RSA 4096]----+
    | . E=.oo|
    | . . .o*++ |
    | o o.*.* |
    | * ++=oB |
    | . S+=++ . |
    | . .*.... |
    | ... . o |
    | .. .o+ |
    | ..oo== |
    +----[SHA256]-----+
  • Verify the following two files have been generated

    1
    2
    3
    johndoe@laptop:~$ ls -la .ssh
    -rw------- 1 johndoe staff 3243 23 apr 16:39 id_johndoe_rsa
    -rw-r--r-- 1 johndoe staff 743 23 apr 16:39 id_johndoe_rsa.pub
  • Copy the contents of the public key to the clipboard

    1
    $ cat .ssh/id_johndoe_rsa.pub | pbcopy
  • Go the the Github and click on Settings.

Edit your github settings
  • Next click on ‘SSH and GPG keys’ in the left menu. Now click on the button ‘New SSH key’.

    Fill in a title like ‘SSH key of John Doe’ and paste the string that you copied into
    the clipboard before’ which should look a bit like below.

1
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDddmyo4+UtzzUju6GwBGQu6NatFVmnDSZrykeqnXLSFT6JY1Ky57x62h80eFkmewauqnVZO09H8klkFMXDeuoweGeYFqowG+c5XwKsTIr4hC5Zxw+9CRa71xk7Xppdc4ROVZsbHVgb4F1kPkcNe4O9R+lyZqRNOb1clwngPfusyzH1WWl60fqhyU9n7WlqwdJ/ih9so8FgvmPYM27uKexOtcF7YWlXYnCLN1n8eRdGvTN01fRtON7zwxj3CyB+qD1JXfumneP0bcYSvBpVNZFDcRBclP7KluvEFYWDaZ8GH7xLJtrf34rIACE7LOFRWFYSTz5BI66794BGhastyczRcPZOzWa1ZQSsdB5iNLX8U32ZLqI71rA8tiViVGqpcVGkCRrl/QetdMEuebf67clXd4bp+AC8753UYfeIYAp0+uAT9zwnZWoBg3JW+RtwzeG7Hg8N3WnB6ULU0PqnA2fUtu0Fm4LpK9YcmRJ+izr6SlcZH87nK/rLcqv99vJSEaIcJz4+IBgZ8MKzfIZ5thritMD8xbKqMkGl1D6oG2ggR+WvnVgSlzdczHXreDy/KHWFDvTH/Vivta5EuOo7mRPkSAB2Ml809ev0+Ul6UvyJXYdTu5+ne5KzSHYWZV+C2lsT/aDT2gmsKNhlyPZOgwvn6IGinTKbO40QEjYozkPxFw== johndoe@gmail.com
  • Create a new .ssh/config file or append to existing config file following host
    1
    2
    3
    4
    Host github.com-johndoe
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_johndoe_rsa

4. Make sure you have NPM and NodeJs installed

5. Make sure you have git installed

6. Install Hexo

1
2
3
4
5
$ npm install hexo-cli -g
$ hexo init blog
$ cd blog
$ npm install
$ hexo server

Browse to http://localhost:4000 and verify you see the initial empty blog. To shutdown the server again just press ctrl-c

7. Look for a theme you like to use and install it.

I chose the icarus theme. To install it change directory into your newly created blog folder and clone the theme using git

1
$ git clone https://github.com/ppoffice/hexo-theme-icarus.git themes/icarus

8. Adapt the following in the file “blog/_config.yml”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Site
title: Pelssers Consultancy
author: Robby Pelssers
language: en
# URL
url: http://pelssersconsultancy.github.io/
# Writing
new_post_name: :year-:month-:day-:title.md # File name of new posts
post_asset_folder: true
## Themes: https://hexo.io/themes/
theme: icarus
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo: git@github.com-johndoe:johndoe/johndoe.github.io.git
branch: master

9. Rename the file ‘blog/themes/icarus/_config.yml.example’ to ‘_config.yml’

10. Adapt the file ‘blog/themes/icarus/_config.yml’ to your needs. E.g.:

11. Create your first blog post

1
2
robbypelssers1@Macbook-Robby-Pelssers:~/Documents/pelssersconsultancy/blog$ hexo new "My First Blog Post"
INFO Created: ~/Documents/pelssersconsultancy/blog/source/_posts/2016-04-23-My-First-Blog-Post.md

As you can see a skeleton markdown file gets generated.

The markdown for this first blog article looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# How to setup your own blog
### 1. Create a new github account
Navigate to [Github] and click the Signup button. It is important to choose the same
username as the one you want to use for [**username**].github.io
Let's assume you created a github account with username [**johndoe**] and email address [**johndoe@gmail.com**].
### 2. Create a new repository
[Github pages] explains how to setup your own blog site, so you can check it for reference.
Log in on [Github] and click on the button 'New Repository'.
Now you should see a similar page like below with Owner [**johndoe**]. Fill in [**johndoe.github.io**] as repository name.
{% asset_img creating_repository.png Creating github repository %}
### 3. Setup you ssh key for github
Github is a versioning system where each different version of any file belonging to your site will be saves and tracked.
...
...
...
[Github]: https://github.com
[Github pages]: https://pages.github.com
[pelssersconsultancy]: http://pelssersconsultancy.github.io
[NodeJS]: https://nodejs.org
[NPM]: https://www.npmjs.com/
[git]: https://git-scm.com/download
[Hexo]: https://hexo.io/
[theme]: https://hexo.io/themes/
[icarus]: https://github.com/ppoffice/hexo-theme-icarus
[markdown]: https://guides.github.com/features/mastering-markdown/

12.Generate the site

1
$ hexo generate

13. Deploy your site to github

1
$ hexo deploy
Share Comments