jshen
jshen

Reputation: 11927

How to manage development, production, testing configuraions with maven?

I'm new to Maven and coming from a Rails background. At a high level, I want to connect to a different database if I'm running tests, running the app locally, and when I deploy to production.

Here's what I was thinking. When I run mvn test it should use test.properties, when I run mvn jetty:run locally it should use dev.properties, and when I deploy it should pickup a properties file that is already on the server, and not in the WAR.

But I have no idea how this is typically done in a Java project built with Maven.

Upvotes: 2

Views: 5150

Answers (3)

CoolBeans
CoolBeans

Reputation: 20820

You can do that by using maven profiles. You would set up a different profile for different environment, data set, etc and activate them based on the environment you are building against.

Upvotes: 6

Shannon
Shannon

Reputation: 1163

For your tests, you could put a test-only Spring config file or property file into the test resources of a module, and have that module generate a test-jar. Then reference that test-jar as a test scope dependency from any modules that need it during testing.

For running in dev mode vs. production mode, one possible non-Maven-related solution would be to make use of an environment variable that specifies a different Spring config/properties file/xml file to use.

Upvotes: 3

Ravi Wallau
Ravi Wallau

Reputation: 10493

We use profiles to distinguish between the different build phases, and we use the assembly plugin with different assembly descriptors for each profile. We have something like the code below. Our default profile is development, but when we release the "release" profile is activated automatically. The assembly descriptors are pretty simple and we have one "common" directory structure, one for development, and one for release.

  <build>
    <plugins>

      <!-- Enable Java 6 features. -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>6</source>
          <target>6</target>
        </configuration>
      </plugin>

      <!-- Configure for development environment. -->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>package</id>
            <phase>package</phase>
            <goals>
              <goal>assembly</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptors>
            <descriptor>src/main/assembly/dist-dev.xml</descriptor>
          </descriptors>
        </configuration>
      </plugin>

      <!-- Deployment configuration. -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-deploy-plugin</artifactId>
        <configuration>
          <groupId>${project.groupId}</groupId>
          <artifactId>${project.artifactId}</artifactId>
          <version>${project.version}</version>
          <packaging>zip</packaging>
          <file>target/${project.build.finalName}.zip</file>
          <url>${project.distributionManagement.snapshotRepository.url}</url>
        </configuration>
      </plugin>

      <!-- Release configuration. -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-release-plugin</artifactId>
        <configuration>
          <goals>assembly:assembly deploy:deploy-file</goals>
          <useReleaseProfile>false</useReleaseProfile>
          <arguments>-Prelease</arguments>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.apache.maven.scm</groupId>
            <artifactId>maven-scm-api</artifactId>
            <version>1.4</version>
          </dependency>
          <dependency>
            <groupId>org.apache.maven.scm</groupId>
            <artifactId>maven-scm-provider-bazaar</artifactId>
            <version>1.4</version>
          </dependency>
        </dependencies>
      </plugin>

    </plugins>
  </build>
  <profiles>

    <!-- Release profile. -->
    <profile>
      <id>release</id>
      <build>
        <plugins>

          <!-- Artifact to deploy is assembled zip file. -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <groupId>${project.groupId}</groupId>
              <artifactId>${project.artifactId}</artifactId>
              <version>${project.version}</version>
              <packaging>zip</packaging>
              <file>target/${project.build.finalName}-bin.zip</file>
              <url>${project.distributionManagement.repository.url}</url>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>

          <!-- Configure for release environment. -->
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
              <descriptors>
                <descriptor>src/main/assembly/dist-rel.xml</descriptor>
              </descriptors>
            </configuration>
          </plugin>

        </plugins>
      </build>
    </profile>

  </profiles>

Upvotes: 6

Related Questions