Tag Archives: Java

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

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

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

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 – 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 – A simple Maven project

I would like to setup the TestNG in Maven so i take this chance to revise the Maven basics.

1. The project should have the following folder structure.

- testng-example (project root folder)
  - src
    - main
      - java
        - info/ykyuen/eureka <folders for package name>
          - Addition.java
      - resources
        - <any resource files like .properties>
  - pom.xml

Continue reading

Apache Ivy – Publish to Artifactory

Previously we have talked about how to work with Apache Ivy and Artifactory.

 

The next thing i would like to share is to publish your built .jar file to Artifactory.

1. Login to Artifactory web portal and create a new repository, click on the Admin tab and select Repository on the left menu side bar.
Continue reading