Automatically detect 'development' profile
Detect when an application is running in development (by the presence of a build file) and automatically add a 'development' profile. Additional detectors can be developed by implementing the `ProfileDetector` interface and registering with the `SpringApplication` Fixes gh-296pull/302/head
parent
643295cc3c
commit
a97bcfe3cd
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* 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.springframework.boot;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link ProfileDetector} that attempts to detect when the application is being developed
|
||||
* and adds a 'development' profile.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class DevelopmentProfileDetector implements ProfileDetector {
|
||||
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private static final String DEFAULT_PROFILE_NAME = "development";
|
||||
|
||||
private static final String EXECUTABLE_JAR_CLASS = "org.springframework.boot.loader.Launcher";
|
||||
|
||||
private static final String[] DEVELOPMENT_TIME_FILES = { "pom.xml", "build.gradle",
|
||||
"build.xml" };
|
||||
|
||||
private final String profileName;
|
||||
|
||||
public DevelopmentProfileDetector() {
|
||||
this(DEFAULT_PROFILE_NAME);
|
||||
}
|
||||
|
||||
public DevelopmentProfileDetector(String profileName) {
|
||||
Assert.notNull(profileName, "ProfileName must not be null");
|
||||
this.profileName = profileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addDetectedProfiles(ConfigurableEnvironment environment) {
|
||||
if (!isPackageAsJar() && isRunningInDevelopmentDirectory()) {
|
||||
environment.addActiveProfile(this.profileName);
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean isPackageAsJar() {
|
||||
if (ClassUtils.isPresent(EXECUTABLE_JAR_CLASS, null)) {
|
||||
this.logger.debug("Development profile not detected: "
|
||||
+ "running inside executable jar");
|
||||
return true;
|
||||
}
|
||||
String command = System.getProperty("sun.java.command");
|
||||
if (StringUtils.hasLength(command) && command.toLowerCase().contains(".jar")) {
|
||||
this.logger.debug("Development profile not detected: started from a jar");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isRunningInDevelopmentDirectory() {
|
||||
File userDir = getUserDir();
|
||||
if (userDir != null && userDir.exists()) {
|
||||
for (String developementTimeFile : DEVELOPMENT_TIME_FILES) {
|
||||
if (new File(userDir, developementTimeFile).exists()) {
|
||||
this.logger.debug("Development profile detected: file "
|
||||
+ developementTimeFile + " present");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected File getUserDir() {
|
||||
String userDir = System.getProperty("user.dir");
|
||||
return (userDir == null ? null : new File(userDir));
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.springframework.boot;
|
||||
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
/**
|
||||
* Strategy interface that can be used to detect active profiles.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public interface ProfileDetector {
|
||||
|
||||
/**
|
||||
* Add any detected profiles to the specified environment.
|
||||
* @param environment the environment
|
||||
*/
|
||||
void addDetectedProfiles(ConfigurableEnvironment environment);
|
||||
|
||||
}
|
@ -0,0 +1,128 @@
|
||||
/*
|
||||
* 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.springframework.boot;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
/**
|
||||
* Tests for {@link DevelopmentProfileDetector}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class DevelopmentProfileDetectorTests {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
private TestDevelopmentProfileDetector detector;
|
||||
|
||||
private ConfigurableEnvironment environment;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.detector = new TestDevelopmentProfileDetector();
|
||||
this.environment = mock(ConfigurableEnvironment.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notFound() {
|
||||
this.detector.addDetectedProfiles(this.environment);
|
||||
verifyZeroInteractions(this.environment);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void foundDueToMavenBuild() throws Exception {
|
||||
this.temporaryFolder.newFile("pom.xml").createNewFile();
|
||||
this.detector.addDetectedProfiles(this.environment);
|
||||
verify(this.environment).addActiveProfile("development");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void foundDueToGradleBuild() throws Exception {
|
||||
this.temporaryFolder.newFile("build.gradle").createNewFile();
|
||||
this.detector.addDetectedProfiles(this.environment);
|
||||
verify(this.environment).addActiveProfile("development");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void foundDueToAntBuild() throws Exception {
|
||||
this.temporaryFolder.newFile("build.xml").createNewFile();
|
||||
this.detector.addDetectedProfiles(this.environment);
|
||||
verify(this.environment).addActiveProfile("development");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void differentProfileName() throws Exception {
|
||||
this.detector = new TestDevelopmentProfileDetector("different");
|
||||
this.temporaryFolder.newFile("pom.xml").createNewFile();
|
||||
this.detector.addDetectedProfiles(this.environment);
|
||||
verify(this.environment).addActiveProfile("different");
|
||||
verifyNoMoreInteractions(this.environment);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notFoundWhenStartedFromJar() throws Exception {
|
||||
System.setProperty("sun.java.command", "something.jar");
|
||||
this.temporaryFolder.newFile("pom.xml").createNewFile();
|
||||
this.detector.setSkipPackageAsJar(false);
|
||||
this.detector.addDetectedProfiles(this.environment);
|
||||
verifyZeroInteractions(this.environment);
|
||||
}
|
||||
|
||||
private class TestDevelopmentProfileDetector extends DevelopmentProfileDetector {
|
||||
|
||||
private boolean skipPackageAsJar = true;
|
||||
|
||||
public TestDevelopmentProfileDetector() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TestDevelopmentProfileDetector(String profileName) {
|
||||
super(profileName);
|
||||
}
|
||||
|
||||
public void setSkipPackageAsJar(boolean skipPackageAsJar) {
|
||||
this.skipPackageAsJar = skipPackageAsJar;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isPackageAsJar() {
|
||||
if (this.skipPackageAsJar) {
|
||||
// Unfortunately surefire uses a jar so we need to stub this out
|
||||
return false;
|
||||
}
|
||||
return super.isPackageAsJar();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected File getUserDir() {
|
||||
return DevelopmentProfileDetectorTests.this.temporaryFolder.getRoot();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue