Posts

Showing posts from March, 2014

Maven PermGen space OutOfMemoryError issue

If you get something like [ERROR] PermGen space -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: ...try doing this to resolve this OutOfMemoryError issue:  export MAVEN_OPTS="-Xmx1g -XX:MaxPermSize=512m"

@Inject vs. @Resource vs. @Autowired — Java vs. Spring

@Autowired (Spring) & @Inject (Java) Matches by type Restricts by qualifiers Matches by name @Resource   (Java) Matches by name Matches by type Restricted by qualifiers, ignored if match is found by name

Replace SourceTree by CLI Git, branch names in "git log"

A graphical representation of a multi-branch commit history is usually one of the most used features of GUI tools like SourceTree. You can get the same thing in a much simpler way using git log --graph --all --decorate So you will see all branches, their merge commits and the branches that have been merged including a visual branch graph.

Maven: No plugin found for prefix 'jetty' in the current project...

 If you get something like [ERROR] No plugin found for prefix 'jetty' in the current project and in the plugin groups [de.hybris.mavenplugins, org.codehaus.cargo, de.hybris.platform, com.sap.research, org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/alex/.m2/repository), central (http://repo1.maven.org/maven2), hybris-repository (http://repository.hybris.com/hybris-repository)] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException add something like the following to your ~/.m2/settings.xml <pluginGroups>     <pluginGroup>org.mortbay.jetty</pluginGroup> </pluginGroups> ...to r

Git branch name in your prompt

When you want to get something similar like idea:~/my/project/hybris/order-service (develop)   as your Bash prompt on a Mac or perhaps some other Unix OSs — where "( develop )" could be your  currently checked out Git branch — add this PS1='\h:\[\e[0;31m\]\w\[\e[m\]`__git_ps1` '   or  PS1='\h:\[\e[0;31m\]\w\[\e[m\] \e[1;31m\]$(__git_ps1 %s)\e[m '  to your ~/.bash_profile Alternatively you could use PS1='`__git_ps1` ' just to see the branch name.

NPE killer in Java8 to prevent NullPointerExceptions

In JDK8 the Optional<T> type monad has been introduced. The purpose of this class is essentially to facilitate the active thinking about the case when null  might be assigned to an object causing a NullPointerException when this object will be dereferenced. Consider this: public static Optional<Drink> find(String name, List<Drink> drinks) {     for(Drink drink : drinks) {     if(drink.getName().equals(name)) {     return Optional.of(drink);     }   }   return Optional.empty(); } List<Drink> drinks = Arrays.asList(new Drink("Beer"), new Drink("Wine"), new Drink("Cocktail")); Optional<Drink> found = find("Beer", drinks); if(found.isPresent()) {   Drink drink = found.get();   String name = drink.getName(); }

Delete a remote / local Git branch

git branch -d your_local_branch_to_delete  # use the simple name git push origin : your_ remote _branch_to_delete # use the simple name

Mark conflicting files as resolved in Git

After you've manually resolved a Git merge conflict, you have to mark the resulting file as "resolved" (notion from other DVCSs). Just do this to achieve this: git add your_file_with_manually_resolved_conflicts.txt

Generics: difference between a wildcard vs Object

The short version is: Collection<Object> != (Collection<?, ?> == Collection) or Map<Object, Object> != (Map<?, ?> == Map) Why? Using just  Collection  is — since Java 5 & the introduction of Generics — a short form for Collection<?> collectionOfUnknown   which is not the same as Collection<Object> collectionOfObjects  because you can assign any collection to  collectionOfUnknown   like Collection<Foo> collectionOfFoos , Collection<Bar> collectionOfBars or Collection<Object> collectionOfObjects  but you cannot assign Collection<Foo> collectionOfFoos to a Collection<Object>  collectionOfObjects .

Speed up your Maven development

A regular Maven execution looks something like: mvn test package Executing mvn test package -T 8 --offline  instead, should significantly reduce the execution time. -T 8  In this case 8 is the number of threads of your CPU. Intel i7 CPUs have 8 threads. --offline This flag tells Maven to work in offline mode . So you can spare the time consuming HTTP requests.

Debug Jetty / Tomcat applications using Maven

Simply use the mvnDebug  instead for the usual mvn  command and you should see sth. like: idea:~/my/project/loxal/your-service mvnDebug jetty:run Preparing to Execute Maven in Debug Mode Listening for transport dt_socket at address: 8000 Now you can simply attach the debugger to port 8000 and you are good to go.

Java's Types: Primitives vs Reference Types

In Java there are essentially two different kinds of types. The primitive type denotes non-nullable types like boolean, byte, sbyte, short, integer, float, and double . The reference type denotes all nullable types, typically known as "objects" which always have " Object " as their explicit or implicit super class, e.g. Integer, Double, YourVeryOwnBusinessObject, BusinessDao, PersonDto, RuntimeException, or Exception. Actually null  is a literal of the "null" type which has no name but can be assigned or cast to any reference type . However it cannot be assigned to primitive types. By default all reference types are initialized with null .

Best practice logging implementation for Java — Logback

pom.xml:         <dependency>             <groupId>ch.qos.logback</groupId>             <artifactId>logback-classic</artifactId>             <version>1.1.1</version>             <scope>compile</scope>         </dependency> src/main/java/resources/logback.xml: <?xml version="1.0" encoding="UTF-8"?> <configuration>     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">         <encoder>             <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>         </encoder>     </appender>     <root level=" info ">         <appender-ref ref="STDOUT"/>     </root> </configuration> Calling the logging facility from within Java code:  private static final Logger LOG = LoggerFactory.getLogger(YourClass.class); // is not shown because the log

All Java8 keywords vs literals

These are the 50 Java keywords that are not allowed to be used as identifiers: abstract, continue, for, new, switch, assert, default, if, package, synchronized, boolean, do, goto , private, this, break, double, implements, protected, throw, byte, else, import, public, throws, case, enum, instanceof, return, transient, catch, extends, int, short, try, char, final, interface, static, void, class, finally, long, strictfp, volatile, const , float, native, super, while The const and goto  keywords are reserved and not currently used. You might wonder why  null, true, and false  have not been mentioned in the list. Those are literals and no keywords. Literals are also in-source values like "this is a string" (String literal) or numbers like " 1.234_456d " (double literal).

How to revert a Git commit

Locally, if  you do not have pushed your local remote to any remote Git repository: git reset --hard <revision_id_of_the_commit_you_would_like_to_reset_to> or just add the --ammed  flag git commit --amend -m "This commit will add additional/changed content to your previous local commit" Remotely, if you already pushed your commit to a remote Git repository: git reset --hard <revision ID of a commit you would like to overwrite the HEAD commit of a GIT repository> git push -f