Customize active profiles via a dedicated property

This commit adds a dedicated property to specify the active profiles to
use when running an application via the Maven plugin. This works also
on the command line using the `run.profiles` system property and is
consistently applied whether the process is forked or not.

Closes gh-4199
pull/4202/head
Stephane Nicoll 9 years ago
parent f53720ea0e
commit 4eefd92e82

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>run-profiles</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<fork>true</fork>
<profiles>
<profile>foo</profile>
<profile>bar</profile>
</profiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,36 @@
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.test;
import java.util.Arrays;
public class SampleApplication {
public static void main(String[] args) {
if (args.length < 1) {
throw new IllegalArgumentException("Missing active profile argument " + Arrays.toString(args) + "");
}
String argument = args[0];
if (!argument.startsWith("--spring.profiles.active=")) {
throw new IllegalArgumentException("Invalid argument " + argument);
}
int index = args[0].indexOf("=");
String profile = argument.substring(index + 1, argument.length());
System.out.println("I haz been run with profile(s) '" + profile + "'");
}
}

@ -0,0 +1,2 @@
def file = new File(basedir, "build.log")
return file.text.contains("I haz been run with profile(s) 'foo,bar'")

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>run-profiles</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<profiles>
<profile>foo</profile>
<profile>bar</profile>
</profiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,36 @@
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.test;
import java.util.Arrays;
public class SampleApplication {
public static void main(String[] args) {
if (args.length < 1) {
throw new IllegalArgumentException("Missing active profile argument " + Arrays.toString(args) + "");
}
String argument = args[0];
if (!argument.startsWith("--spring.profiles.active=")) {
throw new IllegalArgumentException("Invalid argument " + argument);
}
int index = args[0].indexOf("=");
String profile = argument.substring(index + 1, argument.length());
System.out.println("I haz been run with profile(s) '" + profile + "'");
}
}

@ -0,0 +1,2 @@
def file = new File(basedir, "build.log")
return file.text.contains("I haz been run with profile(s) 'foo,bar'")

@ -53,6 +53,8 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
private static final String SPRING_LOADED_AGENT_CLASSNAME = "org.springsource.loaded.agent.SpringLoadedAgent";
private static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active";
/**
* The Maven project.
* @since 1.0
@ -100,6 +102,15 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
@Parameter(property = "run.arguments")
private String[] arguments;
/**
* The spring profiles to activate. Convenience shortcut of specifying the
* 'spring.profiles.active' argument. On command line use commas to separate
* multiple profiles.
* @since 1.3
*/
@Parameter(property = "run.profiles")
private String[] profiles;
/**
* The name of the main class. If not specified the first compiled class found that
* contains a 'main' method will be used.
@ -239,7 +250,9 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
* @return a {@link RunArguments} defining the application arguments
*/
protected RunArguments resolveApplicationArguments() {
return new RunArguments(this.arguments);
RunArguments runArguments = new RunArguments(this.arguments);
addActiveProfileArgument(runArguments);
return runArguments;
}
private void addArgs(List<String> args) {
@ -274,6 +287,21 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
}
}
private void addActiveProfileArgument(RunArguments arguments) {
if (this.profiles.length > 0) {
StringBuilder sb = new StringBuilder("--")
.append(ACTIVE_PROFILES_PROPERTY).append("=");
for (int i = 0; i < this.profiles.length; i++) {
sb.append(this.profiles[i]);
if (i < this.profiles.length - 1) {
sb.append(",");
}
}
arguments.getArgs().addFirst(sb.toString());
logArguments("Active profile(s): ", this.profiles);
}
}
private void addClasspath(List<String> args) throws MojoExecutionException {
try {
StringBuilder classpath = new StringBuilder();

@ -0,0 +1,47 @@
-----
Specify active profiles
-----
Stephane Nicoll
-----
2014-07-07
-----
The active profiles to use for a particular application can be specified using the <<<profiles>>>
argument. The following configuration enables the foo and bar profiles:
---
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<configuration>
<profiles>
<profile>foo</profile>
<profile>bar</profile>
</profiles>
</configuration>
...
</plugin>
...
</plugins>
...
</build>
...
</project>
---
The profiles to enable can be specified on the command line as well, make sure to separate them with
a comma, that is:
---
mvn spring-boot:run -Drun.profiles=foo,bar
---

@ -48,6 +48,8 @@ Spring Boot Maven Plugin
* {{{./examples/it-random-port.html}Random port for integration tests}}
* {{{./examples/run-profiles.html}Specify active profiles}}
[]

@ -116,7 +116,8 @@ mvn spring-boot:run
If you need to specify some JVM arguments (i.e. for debugging purposes), you can use
the <<<jvmArguments>>> parameter, see {{{./examples/run-debug.html}Debug the application}}
for more details.
for more details. As a convenience, the profiles to enable are handed by a specific
property (<<<profiles>>>), see {{{./examples/run-profiles.html}Specify active profiles}}.
By default, any <<src/main/resources>> folder will be added to the application classpath
when you run the application and any duplicate found in <<target/classes>> will be

@ -11,6 +11,7 @@
<item name="Exclude a dependency" href="examples/exclude-dependency.html"/>
<item name="Debug the application" href="examples/run-debug.html"/>
<item name="Random port for integration tests" href="examples/it-random-port.html"/>
<item name="Specify active profiles" href="examples/run-profiles.html"/>
</menu>
<menu ref="reports"/>
</body>

Loading…
Cancel
Save