AspectJ and Maven configuration

Here is a maven configuration for AspectJ weaving. We use here the “codehaus” maven plugin for AOP Weaving with AspectJ 1.6.x and Java 1.6 versions.

maven dependencies

<dependencies>
...
  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.6.11</version>
  </dependency>
...
  <dependency>
    <groupId>my-artifact-to-weave.groupId<groupId>
    <artifactId>my-artifact-to-weave.artifactId</artifactId>
    <version>my-artifact-to-weave.version</version>
  </dependency>
...
</dependencies>

maven compiler plugin configuration

In order to use annotated based pointcuts and advices definitions.

<build>
  <plugins>
    ...
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.5.1</version>
      <configuration>
        <target>1.6</target>
        <source>1.6</source>
      </configuration>
    </plugin>
    ...
  </plugins>
</build>

maven AspectJ plugin configuration

<build>
  <plugins>
    ...
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.4</version>
      <configuration>
        <weaveDependencies>
          <weaveDependency>
            <groupId>my-artifact-to-weave.groupId<groupId>
            <artifactId>my-artifact-to-weave.artifactId</artifactId>
          </weaveDependency>
        </weaveDependencies>
      </configuration>
      <executions>
        <execution>
          <id>compile</id>
          <configuration>
            <source>1.6</source>
          </configuration>
          <goals>
            <goal>compile</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
</build>
Advertisements

Loading POM version in your Java application

This can be done by writing your pom version into the Manifest file and then loading your manifest in your java code. Note that this will not work when running your code from eclipse IDE.

pom plugin configuration
Using the maven archiver plugin to write pom information into the application Manifest:

<build>
 <plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
            </manifest>
        </archive>
    </configuration>
  </plugin>
 </plugins>
</build>

Loading and reading Manifest
Simply access the resource using the Manifest class

    Manifest manifest = new Manifest(this.getClass().getResourceAsStream("/META-INF/MANIFEST.MF"));
    String version = manifest.getMainAttributes().getValue("Implementation-Version");

SVN, Archiva and maven-release-plugin configuration

Here is a standard SVN and Maven layout / configuration to perform maven releases with minimal configuration.

Project layout on SVN

SVN SERVER
project-artifactId
branches
tags
trunk

Maven POM configuration

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ichir.projects</groupId>
    <artifactId>project-artifactId</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <scm>
        <developerConnection>scm:svn:__HTTP_PATH_TO_SVN__/project-artifactId/trunk</developerConnection>
    </scm>
    <distributionManagement>
        <repository>
            <id>internal</id>
            <url>__HTTP_PATH_TO_ARCHIVA__/repository/internal/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <url>__HTTP_PATH_TO_ARCHIVA__/repository/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-release-plugin</artifactId>
                <configuration>
                    <tagBase>__HTTP_PATH_TO_SVN__/project-artifactId/tags</tagBase>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Perform a release

    mvn release:prepare

then if succeeded

    mvn release:preform

Reading Java Derby Metadata

Find in this post examples on how to read and retrieve a Derby database meta-data information to get list of schemas, tables, stored procedures, …

Get schemas

    DatabaseMetaData dmd = connection.getMetaData();
    ResultSet rs = dmd.getSchemas();
    List<String> schemas = new ArrayList<String>();
    while (rs.next()) {
        schemas.add(rs.getString("TABLE_SCHEM"));
    }
    rs.close();

“TABLE_SCHEM” is not a syntax error.

Get stored procedures

    // get database metadata
    DatabaseMetaData metaData = connection.getMetaData();
    // get procs
    ResultSet rs = metaData.getProcedures(null, null, "%");
    List<String> procs = new ArrayList<String>();
    while (rs.next()) {
        procs.add(rs.getString(3));
    }
    rs.close();

Get schema triggers

    // get triggers
    String query = String.format(
        "select a.TRIGGERNAME from SYS.SYSTRIGGERS a inner join SYS.SYSSCHEMAS b on a.SCHEMAID = b.SCHEMAID where b.SCHEMANAME = '%s' ORDER BY a.TRIGGERNAME",
        "my_schema"
    );
    Statement st = connection.createStatement();
    ResultSet rs = st.executeQuery(query);
    List<String> triggers = new ArrayList<String>();
    while (rs.next()) {
        triggers.add(rs.getString(1));
    }
    rs.close();
    st.close();

Get schema tables

    // get database metadata
    DatabaseMetaData metaData = connection.getMetaData();
    // get columns
    ResultSet rs = metaData.getTables(null, "my_schema", "%", null);
    List<String> tables = new ArrayList<String>();
    while (rs.next()) {
        // 1: none
        // 2: schema
        // 3: table name
        // 4: table type (TABLE, VIEW) 
        tables.add(rs.getString(3));
    }
    rs.close();

or

String query = String.format(
    "select a.TABLENAME from SYS.SYSTABLES a inner join SYS.SYSSCHEMAS b on a.SCHEMAID = b.SCHEMAID where b.SCHEMANAME = '%s' ORDER BY a.TABLENAME",
    "my_schema"
);
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery(query);
List<String> tables = new ArrayList<String>();
while (rs.next()) {
    tables.add(rs.getString(1));
}
rs.close();
st.close();

Get table columns

    // get data base metadata
    DatabaseMetaData metaData = connection.getMetaData();
    // get columns
    ResultSet rs = metaData.getColumns(null, "my_schema", "my_table", "%");
    List<String> columns = new ArrayList<String>();
    while (rs.next()) {
        // 1: none
        // 2: schema
        // 3: table name
        // 4: column name
        // 5: length
        // 6: data type (CHAR, VARCHAR, TIMESTAMP, ...)
        columns.add(rs.getString(4));
    }
    rs.close();

Get table indexes

    // get data base metadata
    DatabaseMetaData metaData = connection.getMetaData();
    // get indexes
    ResultSet rs = metaData.getIndexInfo(null, "my_schema", "my_table", false, true);
    List<String> indexes = new ArrayList<String>();
    while (rs.next()) {
        // 1: type (index)
        // 2: schema
        // 3: table
        // 4: TODO
        // 5: 
        // 6: index name
        indexes.add(rs.getString(6));
    }
    rs.close();

Execute a delete/update SQL statement

    Statement stmtDerby = connection.createStatement();
    ResultSet executeQuery = stmtDerby.executeQuery(statement);

Execute a select SQL statement

    Statement stmtDerby = connection.createStatement();
    ResultSet executeQuery = stmtDerby.executeQuery(statement);
    if (executeQuery.next()) {
        // first result
        value1 = executeQuery.getString(1);
        // second result
        value2 = executeQuery.getString(2);
        // ... etc
    }
    stmtDerby.close();