Tag Archives: Java

Maven – Include system scope dependency in maven-assembly-plugin

If you don’t have Maven repository server like Archiva or Nexus, you may need to include those libs which are not public on the internet in your Maven project and specifiy the dependency scope as system in the pom.xml. The following example includes the eureka-0.0.1.jar which is located in the folder called external on project root.

...
<dependency>
  <groupId>ykyuen.info</groupId>
  <artifactId>eureka</artifactId>
  <version>0.0.1</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/external/eureka-0.0.1.jar</systemPath>
</dependency>
...

 

A few months ago i wrote a blog post about how to package the project jar file with all dependencies.
Maven – Package the jar file with dependencies
Continue reading Maven – Include system scope dependency in maven-assembly-plugin

Java – Get current Unix timestamp from date string

The following piece of Java code could convert a date string into a Unix timestamp.

String dateString = "Fri, 09 Nov 2012 23:40:18 GMT";
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z");
Date date = dateFormat.parse(dateString);
long unixTime = (long)date.getTime()/1000;
System.out.println(unixTime); //<- prints 1352504418

 

Done =)

Reference:

http://stackoverflow.com/questions/13392030/unix-timestamp-creation-in-java

About these ads

Maven – Add SVN revision number to the name of your built artifact

The svn-revision-number-maven-plugin could help you adding the SVN revision number to your built artifact. Configure your pom.xml as follow.

...
<build>
  <finalName>${project.artifactId}-r${svn.revision}</finalName>
  <plugins>
    <!-- Use the svn revision number for the built artifact -->
    <plugin>
      <groupId>com.google.code.maven-svn-revision-number-plugin</groupId>
      <artifactId>svn-revision-number-maven-plugin</artifactId>
      <version>1.13</version>
      <executions>
        <execution>
          <goals>
            <goal>revision</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <entries>
          <entry>
            <prefix>svn</prefix> <!-- create the ${svn.revision} -->
          </entry>
        </entries>
      </configuration>
    </plugin>
  </plugins>
</build>
...

Continue reading Maven – Add SVN revision number to the name of your built artifact

AspectJ – Plugin execution not covered by lifecycle configuration error in Eclipse

When you open your AspectJ project in Eclipse with the m2eclipse, the following error may appear.

  • Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:aspectj-maven-plugin…

 

This is because m2e doesn’t provide lifecycle mapping for aspectj-maven-plugin. A work around is to add the following <pluginManagement> setting in the pom.xml.
Continue reading AspectJ – Plugin execution not covered by lifecycle configuration error in Eclipse

Selenium – Element not found in the cache

You probably will come across the following error frequently when writing Selenium test case for a Ajax-rich website.

  • Error: Element not found in the cache – perhaps the page has changed since it was looked up

 

This is because the Selenium WebDriver reference to your element would now be stale as the DOM has been modified by Ajax. To resolve the problem in Java, you can make us of the WebDriverWait and find the element each time before you use it.
Continue reading Selenium – Element not found in the cache

Maven – Add Java classpath to manifest for runtime

Previously we have talked about how to package a .jar artifact with dependencies included.

 

An alternative solution is to put your dependencies in the src/main/resources folder and they will be copied to the target folder together with the packaged .jar file. Then we can add the class paths to these dependencies in the Manifest with the help of the maven-jar-plugin.

The following project is a real life example which i use it to test the connection between the JBoss server and the MSSQL server.
Continue reading Maven – Add Java classpath to manifest for runtime

Maven – Package the jar file with dependencies

Sometimes we want to build a executable jar which contains all the dependencies. In this case we could make use of the maven-assembly-plugin which help us to package a jar-with-dependencies artifact during the Maven package phase. Let’s refer to the simple Maven project we did before.

 

1. First let’s introduce a dependency to our project. Say, we want to make use of the Apache Commons – Commons Lang to check the empty string. Edit the src/main/java/info/ykyuen/eureka/Addition.java as follow.
Continue reading Maven – Package the jar file with dependencies