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-4199pull/4202/head
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'")
|
@ -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
|
||||
---
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue