Showing posts with label Plugin. Show all posts
Showing posts with label Plugin. Show all posts

Thursday, 2 August 2012

How to Add License Headers to Code Files via Maven?

This post explains how to automate the inclusion of headers in code files via Maven. We will add the following header (the code sample is available on GitHub) :
/**
 * Copyright (C) 2010-2012 MyCompany - All rights reserved.
 */
This header is based on a template located in src/main/resources/fileHeader.txt:
Copyright (C) ${h_inceptionYear}-${h_currentYear} ${h_copyrightOwner} - All rights reserved.
It contains three ${parameters} which are configured in the pom.xml. Two are defined in the properties:
<properties>
  ...
  <inceptionYear>2010</inceptionYear>
  <copyrightOwner>MyCompany</copyrightOwner>
</properties>
One is computed via a plugin (the current year):
<!-- Creating current year element -->
<plugin>
  <groupId>org.codehaus.groovy.maven</groupId>
  <artifactId>gmaven-plugin</artifactId>
  <version>1.0</version>
  <executions>
    <execution>
      <phase>validate</phase>
      <goals>
        <goal>execute</goal>
      </goals>
      <configuration>
        <source>
          import java.util.Date
          import java.text.MessageFormat
          def vartimestamp = MessageFormat.format("{0,date,yyyy}", new Date())
          project.properties['currentYear'] = vartimestamp
        </source>
      </configuration>
    </execution>
  </executions>
</plugin>
The template location, together with the parameters mapping and the types of files to ignore, is configured here:
  <!-- Adding header to file -->
  <plugin>
    <groupId>com.mycila.maven-license-plugin</groupId>
    <artifactId>maven-license-plugin</artifactId>
    <version>1.8.0</version>
    <configuration>
      <!-- Template location -->
      <header>src/main/resources/fileHeader.txt</header>
      <properties>
        <!-- Values to be substituted in template -->
        <h_inceptionYear>${inceptionYear}</h_inceptionYear>
        <h_currentYear>${currentYear}</h_currentYear>
        <h_copyrightOwner>${copyrightOwner}</h_copyrightOwner>
      </properties>
      <strictCheck>true</strictCheck>
      <excludes>
        <exclude>**/*.html</exclude>
        <exclude>**/*.xml</exclude>
        <exclude>**/*.txt</exclude>
        <exclude>**/*.ec</exclude>
        <exclude>**/*.log</exclude>
        <exclude>**/*.css</exclude>
        <exclude>**/*.js</exclude>
        <exclude>**/*.jsp</exclude>
        <exclude>**/*.md</exclude>
      </excludes>
    </configuration>
    <executions>
      <execution>
        <phase>validate</phase>
        <goals>
          <goal>format</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

During the build process, proper headers are added (or updated) in code files:
/**
 * Copyright (C) 2010-2012 MyCompany - All rights reserved.
 */

package com.mypackage;

public class MyClass {

  // ...

}

Friday, 27 July 2012

How to Deploy a .war to Tomcat 7 via Maven?

In order to deploy a .war to Tomcat 7 during a maven build process, the cargo plugin is very useful.

One should add the following plugin in your pom.xml:
  <plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <version>1.1.2</version>
    <configuration>
      <container>
        <containerId>tomcat7x</containerId>
        <type>remote</type>
      </container>
      <configuration>
        <type>runtime</type>
        <properties>
          <cargo.remote.uri>
            http://localhost:8080/manager/text
          </cargo.remote.uri>
          <cargo.remote.username>admin</cargo.remote.username>
          <cargo.remote.password>admin</cargo.remote.password>
        </properties>
      </configuration>
      <deployer>
        <type>remote</type>
        <deployables>
          <deployable>
            <groupId>com.my-group-id</groupId>
            <artifactId>my-artifact</artifactId>
            <type>war</type>
          </deployable>
        </deployables>
      </deployer>
    </configuration>
  </plugin>

The remote URI is configured to deploy the .war on a local Tomcat 7 installation listening to port 8080. The host could be a remote host too (one should use https instead for security).

The profile used to connect to maven must be configured with proper roles in the /conf/tomcat-users.xml file where Tomcat is installed, as following:
  <tomcat-users>
    <user name="admin" password="admin"
       roles="admin-gui,manager-gui,manager-script" />
    ...
  </tomcat-users>

Then you can use the following maven goals to deploy or redeploy your application:
  cargo:deployer-deploy
  cargo:deployer-redeploy
More Maven tips & tricks here.