04 June 2015

What is Java Code Analyzer?

Analyzing java source code is an important aspest of good coding. It helps developer to findout the unused variables, dead code, deviation from the coding standard, possible bugs and many more. There are many open source Java code analyzers are available in the market. Few of them are PMD, SONAR, Find Bugs, QJ-Pro, JDiff, CheckStyle etc. PMD and SONAR are very popular in the industry.

What is PMD?

PMD is a static rule set based Java source code analyzer that identifies the potential problems like:

  • Possible bugs - Empty try/catch/finally/switch blocks
  • Dead code - Unused variables
  • Duplicate code
  • Empty if/while statements

PMD report shows the indicative issues. It is upto the developer to decide whether the issue to be fixed. However, it is advisable that the developpers should fix priority 1 and 2 violations.

What is SONAR?

SONAR (Currently known as SonarQube) is an open source platform to inspect code quality. It identifies the problems as mentioned in the below image.

Sonar report shows the indicative issues. It is upto the developer to decide whether the issue to be fixed. However, it is advisable that the developpers should fix priority 1 and 2 violations

What is Continuous Integration (CI)?

A complementary practice to CI (Continuous Integration) is that before submitting work, each programmer must do a complete build and run (and pass) all unit tests. Integration tests are usually run automatically on a CI server when it detects a new commit. All programmers should start the day by updating the project from the repository.
Jenkins, Hudson are the CI tools for Java, which runs in a servlet container in Tomcat, Glassfish etc. Hudson was started by Sun Microsystem and later on it was rebranded to Jenkins by Oracle. However, Hudson is currently maintained by Eclipse Foundation.

Maven

Maven is a XML based (pom.xml) build tool primarily for Java. However, maven can also be used for other languages like C#, Ruby, Scala. Maven dynamically downloads Java libraries and Maven plug-ins from one or more repositories such as the Maven 2 Central Repository, and stores them in a local cache (%USER_HOME%/.m2/Repository by default).

Important tags in pom.xml

SL NO tag name Description
1 groupId This is used to identify the project uniquely. In the below example, com.ashish.maven is the group id so the modules under this groupId will get built inside %REPOSITORY_PATH%\com\ashish\maven folder.
2 artifactId Ideally this is the name of the module. The final jar/war will get created by this name.
3 version This indicates the current artifact version
4 packaging This indicates the type of the module/project. Values are pom,war,jar, ear, ejb etc. For the parent module pom should be the packaging type. Similarly, for the war project the packaging is of type war.
5 repository If the added dependencies not found in the local repository then the pom will try to relove the dependency from the URL mentioned in this tag

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>HibernateExample</groupId>
  <artifactId>HibernateExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <repositories>
    <repository>
        <id>jboss</id>
        <url>http://repository.jboss.org/maven2</url>
    </repository>
  </repositories>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
  <dependencies>
  	<!-- Hibernate core -->
	<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.6.Final</version>
    </dependency>
	
	<!-- Hibernate annotation -->
	<dependency>
		<groupId>hibernate-annotations</groupId>
		<artifactId>hibernate-annotations</artifactId>
		<version>3.3.0.GA</version>
	</dependency>
 
	<dependency>
		<groupId>hibernate-commons-annotations</groupId>
		<artifactId>hibernate-commons-annotations</artifactId>
		<version>3.0.0.GA</version>
	</dependency>
	
	<!-- Hibernate library dependecy start -->
	<dependency>
		<groupId>dom4j</groupId>
		<artifactId>dom4j</artifactId>
		<version>1.6.1</version>
	</dependency>
 
	<dependency>
		<groupId>commons-logging</groupId>
		<artifactId>commons-logging</artifactId>
		<version>1.1.1</version>
	</dependency>
 
	<dependency>
		<groupId>commons-collections</groupId>
		<artifactId>commons-collections</artifactId>
		<version>3.2.1</version>
	</dependency>
 
	<dependency>
		<groupId>cglib</groupId>
		<artifactId>cglib</artifactId>
		<version>2.2</version>
	</dependency>
	<!-- Hibernate library dependecy end -->
	
	<!-- HSQL database -->
	<dependency> 
        <groupId>hsqldb</groupId> 
        <artifactId>hsqldb</artifactId> 
        <version>1.8.0.10</version> 
    </dependency>
  </dependencies>
</project>

Maven Repository

Maven Local Repository

Local repository is a local folder used to resolve the required dependencies. New dependencies will get downloaded in this location. Default local repository is %USER_HOME%.m2\repository. However, it can be changed from settings.xml file in %MAVEN_PATH%\conf\ path.

Maven Central Repository

To resolve dependencies, maven checks the local repository. If not found then search in the central repository. The default central repository is : http://repo1.maven.org/maven2/

Maven Remote Repository

The dependencies mentioned in the pom.xml will be searched in the following order

  • Local Repository. If not found then
  • Central Repository. If not found then
  • Remote repository

Suppose your company is developing a multi module application which has 4 JAR and 1 WAR. The WAR has the dependency with 4 JARs which are developed by some other team in your company. Now the other team will publish their JARs in the remote repository which you need to use in your pom.xml as shown below


<repositories>
    <repository>
        <id>company.repository</id>
        <url>http://yourcompany.com/repository</url>
    </repository>
  </repositories>


blog comments powered by Disqus
J2EE,SOAP,RESTful,SVN,PMD,SONAR,JaCoCo,HTTP,API,MAVEN,AngularJS,GitHub,LDAP,AOP,ORM,JMS,MVC,AWS,SQL,PHP,H2DB,JDBC