Archive

Posts Tagged ‘maven’

Seed data for spring roo application.

When developing a spring roo application it can useful to have a mechanism, that creates test data on server startup, so that you have some data for manual testing.

Here I will describe how to accomplice that.

Make a class that implements ApplicationListener.

@Component
public class DataSeeder implements ApplicationListener {

    private final Log log = LogFactory.getLog(DataSeeder.class);

    @Override
    @Transactional
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (event.getApplicationContext().getParent() == null) { // root context
            log.info("event.getApplicationContext() = " + event.getApplicationContext());
            String runMode = "server";
            try {
                Resource resource = new ClassPathResource("/env.properties");
                Properties props = PropertiesLoaderUtils.loadProperties(resource);
                runMode = props.getProperty("run.mode");
            } catch (IOException ignored) {
            }
            log.info("run.mode = " + runMode);
            if ("server".equals(runMode)) {
                // make seed data
                President president = new President();
                president.setName("Ron Paul");
                president.persist();
            }
        }
    }
}

The method onApplicationEvent is called once for each refreshed context. Since an application often has more than one context we must make sure that we only create seed data once. By checking if the context has a parent we can make sure that we only create seed data when the root context is refreshed.

Another issue is that we don’t want to create seed data when unit testing. So we have to find out if we are in test mode or on the server. In the test directory I made a properties file: test/resources/env.properties with one property:
run.mode=test
, this properties file is only copied in the classpath during test. So if we can read its property we should not create seed data. That’s it.

Running tomcat7 with maven2/3

Here I will describe how to setup Maven with Tomcat 7. Also I will show a small Ant script for deploying simple view changes like changes in css, html, js etc.  –  changes that don’t require a rebuild of the war file.

I could let the tomcat-maven-plugin run tomcat for me using the

mvn tomcat:run

, but I already have a tomcat installed, that I want to use. I need to tell the plugin about it in the pom.xml:

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>tomcat-maven-plugin</artifactId>
	<version>1.1</version>
	<configuration>
		<server>local_tomcat7</server>
		<url>http://localhost:8080/manager/text</url>
		<path>/appname</path>
	</configuration>
</plugin>

Maven looks for ‘local_tomcat7’ in ~/.m2/setting.xml, where the username and password for the tomcat manager can be found in the servers section:

<servers>
	<server>
		<id>local_tomcat7</id>
		<username>admin</username>
		<password>mypw</password>
	</server>
</servers>

Last we need to create the manager user in tomcat. This is done in …/apache-tomcat-7.0.12/conf/tomcat-users.xml:

<tomcat-users>
	<user name="admin" password="mypw" roles="manager-script,manager-gui" />
</tomcat-users>

Now the setup is ready for running, deploying and redeploying on tomcat via the tomcat manager.
When working on the view part of the application, it is not really necessary to build a new war file and redeploy it via the manager – its a bit slow. Lets say I only made changes in a java-script file, then I really only need to copy that file directly to …\apache-tomcat-7.0.12\webapps\appname. For this I’m using a small ant script – build.xml:

<?xml version="1.0"?>
<project name="appname" basedir=".">
    <property name="appserver.home" value="C:/java/apache/apache-tomcat-7.0.12"/>
    <property name="deploy.path" value="${appserver.home}/webapps"/>
    <property name="web.dir" value="src/main/webapp"/>
    <property name="name" value="appname"/>
    <target name="copyFilesToServer" description="Deploy files">
        <copy todir="${deploy.path}/${name}" preservelastmodified="true">
            <fileset dir="${web.dir}">
                <include name="**/*"/>
            </fileset>
        </copy>
    </target>
</project>

, with this script it is very fast to deploy simple changes in the view.