Quick Tip: How to Work with GitHub and Multiple Accounts

Tags


Sometimes you may need to have multiple github accounts (one for office, one for work , one for girl friend, whatever) .

Lets say you have ssh keys already setup and github is set for one account. Now for new account:

  • Create a new ssh key

ssh-keygen -t rsa -C "your-email-address"

     Careful while name the file , dont overwrite the existing one .I named it as ~/.ssh/id_rsa_xxx

  • Add the new key to github account

     This is simple enough. Copy the id_rsa_xxx.pub in github > Settings > SSH Keys

  • Create a new config file in  vi ~.ssh/config

        Add following :

#Default GitHub
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa
Host github-COMPANY
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_xxxx
 
You are done . Try it out ..
Vola !! Happy coding !!

Ignore transitive dependency in Maven


Maven helps to manage the dependency of JARS . Most of the time it may happen that same JAR file with different versions is being loaded because some other jar files have it as dependency and thus causing the conflicts .

Typical example is have log4j or slf4j jar with different version in spring-xx.jar and junit-xxx.jar . So when you load you classes an conflict may arise and because of incompatible version of same jar file . For e.g.

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>3.2.1.RELEASE</version>
</dependency>

The above jar uses log4j 1.2.17 (you can check the dependency using mvn dependency:tree).

Consider this log4j jar file is conflicting with some other higher version of log4j which is used by other jar. To resolve this issue we would have to remove the conflicting log4j jar from org.springframework.
it can be done as follows :

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>3.2.1.RELEASE</version>
         <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
</dependency>

Cheers..

Convert JSON to Map or Map to Json in Java

Tags

, , ,


One of the common uses case is to work with Json and use or convert it as java object.

Add the Jackson dependency in pom.xml file

<repositories>
	<repository>
		<id>codehaus</id>
		<url>http://repository.codehaus.org/org/codehaus</url>
	</repository>
  </repositories>
 
  <dependencies>
	<dependency>
		<groupId>org.codehaus.jackson</groupId>
		<artifactId>jackson-mapper-asl</artifactId>
		<version>1.9.12</version>
	</dependency>
  </dependencies>

The Java class :

public static void main(String args[]) {
        ObjectMapper mapper = new ObjectMapper();
        Map m = new HashMap();
        Map personMap = new HashMap<>();
        Map personDetail = new HashMap();
        personDetail.put("firstname", "Bob");
        personDetail.put("lastname", "jackson");
        personDetail.put("age", "12");
        personDetail.put("city", "Berlin");
        
        personMap.put("person", personDetail);
        //convert Map to json string
        try {
            System.out.println(mapper.writeValueAsString(personMap));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // convert json to Map 
        String json = "{\"person\":{\"age\":\"12\",\"lastname\":\"jackson\""
                + ",\"firstname\":\"Bob\",\"city\":\"Berlin\"}}"
        try {
            Map map = mapper.readValue(json, Map.class);
            System.out.println("Map is " + map);
        } catch (Exception e) {
            e.printStackTrace();
        }
 }

Output is

{“person”:{“age”:”12″,”lastname”:”jackson”,”firstname”:”Bob”,”city”:”Berlin”}}
Map is {person={age=12, lastname=jackson, firstname=Bob, city=Berlin}}

Follow

Get every new post delivered to your Inbox.

Join 198 other followers