IDEA-81548 IntelliJ does not recognize Maven profiles activated using a file/exists activation in a multi-module project

This commit is contained in:
Sergey Evdokimov
2012-04-24 14:29:03 +04:00
parent e3e13b4044
commit bfe197be7c
5 changed files with 194 additions and 7 deletions
@@ -17,7 +17,7 @@ package org.jetbrains.idea.maven.model;
import java.io.Serializable;
public class MavenProfileActivation implements Serializable {
public class MavenProfileActivation implements Serializable, Cloneable {
private boolean myActiveByDefault;
private MavenProfileActivationOS myOs;
private String myJdk;
@@ -63,4 +63,14 @@ public class MavenProfileActivation implements Serializable {
public void setFile(MavenProfileActivationFile file) {
myFile = file;
}
@Override
public MavenProfileActivation clone() {
try {
return (MavenProfileActivation)super.clone();
}
catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
}
@@ -437,7 +437,7 @@ public class Maven2ServerEmbedderImpl extends MavenRemoteObject implements Maven
if (expandedProfilesCache == null) expandedProfilesCache = doInterpolate(nativeModel, basedir).getProfiles();
Profile eachExpandedProfile = expandedProfilesCache.get(i);
for (ProfileActivator eachActivator : getProfileActivators()) {
for (ProfileActivator eachActivator : getProfileActivators(basedir)) {
try {
if (eachActivator.canDetermineActivation(eachExpandedProfile) && eachActivator.isActive(eachExpandedProfile)) {
shouldAdd = true;
@@ -471,7 +471,7 @@ public class Maven2ServerEmbedderImpl extends MavenRemoteObject implements Maven
collectProfilesIds(activatedProfiles));
}
private static ProfileActivator[] getProfileActivators() throws RemoteException {
private static ProfileActivator[] getProfileActivators(File basedir) throws RemoteException {
SystemPropertyProfileActivator sysPropertyActivator = new SystemPropertyProfileActivator();
DefaultContext context = new DefaultContext();
context.put("SystemProperties", MavenServerUtil.collectSystemProperties());
@@ -483,7 +483,7 @@ public class Maven2ServerEmbedderImpl extends MavenRemoteObject implements Maven
return new ProfileActivator[0];
}
return new ProfileActivator[]{new FileProfileActivator(),
return new ProfileActivator[]{new MyFileProfileActivator(basedir),
sysPropertyActivator,
new JdkPrefixProfileActivator(),
new OperatingSystemProfileActivator()};
@@ -0,0 +1,118 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* 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.jetbrains.idea.maven.server.embedder;
import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationFile;
import org.apache.maven.model.Profile;
import org.apache.maven.profiles.activation.DetectedProfileActivator;
import org.codehaus.plexus.interpolation.EnvarBasedValueSource;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.MapBasedValueSource;
import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.StringUtils;
import java.io.File;
import java.io.IOException;
/**
* Copied from org.apache.maven.profiles.activation.FileProfileActivator
* added parameter baseDit to test file existing
*
* @author Sergey Evdokimov
*/
public class MyFileProfileActivator extends DetectedProfileActivator
implements LogEnabled
{
private Logger logger;
private final File baseDir;
public MyFileProfileActivator(File baseDir) {
this.baseDir = baseDir;
}
protected boolean canDetectActivation( Profile profile )
{
return profile.getActivation() != null && profile.getActivation().getFile() != null;
}
public boolean isActive( Profile profile )
{
Activation activation = profile.getActivation();
ActivationFile actFile = activation.getFile();
if ( actFile != null )
{
// check if the file exists, if it does then the profile will be active
String fileString = actFile.getExists();
RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
try
{
interpolator.addValueSource( new EnvarBasedValueSource() );
}
catch ( IOException e )
{
// ignored
}
interpolator.addValueSource( new MapBasedValueSource( System.getProperties() ) );
try
{
if ( StringUtils.isNotEmpty(fileString) )
{
fileString = StringUtils.replace( interpolator.interpolate( fileString, "" ), "\\", "/" );
return fileExists(fileString);
}
// check if the file is missing, if it is then the profile will be active
fileString = actFile.getMissing();
if ( StringUtils.isNotEmpty( fileString ) )
{
fileString = StringUtils.replace( interpolator.interpolate( fileString, "" ), "\\", "/" );
return !fileExists(fileString);
}
}
catch ( InterpolationException e )
{
if ( logger.isDebugEnabled() )
{
logger.debug( "Failed to interpolate missing file location for profile activator: " + fileString, e );
}
else
{
logger.warn( "Failed to interpolate missing file location for profile activator: " + fileString + ". Run in debug mode (-X) for more information." );
}
}
}
return false;
}
private boolean fileExists(String path) {
return new File(path).exists() || new File(baseDir, path).exists();
}
public void enableLogging( Logger logger )
{
this.logger = logger;
}
}
@@ -34,8 +34,7 @@ import org.jetbrains.idea.maven.utils.MavenLog;
import org.jetbrains.idea.maven.utils.MavenProcessCanceledException;
import org.jetbrains.idea.maven.utils.MavenUtil;
import java.io.File;
import java.io.IOException;
import java.io.*;
import java.util.*;
import static com.intellij.openapi.util.text.StringUtil.isEmptyOrSpaces;
@@ -440,7 +439,12 @@ public class MavenProjectReader {
// todo: it is a quick-hack here - we add inherited dummy profiles to correctly collect activated profiles in 'applyProfiles'.
List<MavenProfile> profiles = model.getProfiles();
for (MavenProfile each : parentModel.getProfiles()) {
addProfileIfDoesNotExist(new MavenProfile(each.getId(), each.getSource()), profiles);
MavenProfile copyProfile = new MavenProfile(each.getId(), each.getSource());
if (each.getActivation() != null) {
copyProfile.setActivation(each.getActivation().clone());
}
addProfileIfDoesNotExist(copyProfile, profiles);
}
return model;
}
@@ -1063,6 +1063,61 @@ public class StructureImportingTest extends MavenImportingTestCase {
assertModules("project", "m");
}
public void testFileProfileActivationInParentPom() throws Exception {
createProjectPom("<groupId>test</groupId>" +
"<artifactId>project</artifactId>" +
"<version>1</version>" +
"<packaging>pom</packaging>" +
" <profiles>" +
" <profile>" +
" <id>xxx</id>" +
" <dependencies>" +
" <dependency>" +
" <groupId>junit</groupId>" +
" <artifactId>junit</artifactId>" +
" <version>4.0</version>" +
" </dependency>" +
" </dependencies>" +
" <activation>" +
" <file>" +
" <exists>src/io.properties</exists>" +
" </file>" +
" </activation>" +
" </profile>" +
" </profiles>" +
"<modules>" +
" <module>m1</module>" +
" <module>m2</module>" +
"</modules>");
createModulePom("m1", "<groupId>test</groupId>" +
"<artifactId>m1</artifactId>" +
"<parent>" +
" <groupId>test</groupId>" +
" <artifactId>project</artifactId>" +
" <version>1</version>" +
"</parent>");
createModulePom("m2", "<groupId>test</groupId>" +
"<artifactId>m2</artifactId>" +
"<parent>" +
" <groupId>test</groupId>" +
" <artifactId>project</artifactId>" +
" <version>1</version>" +
"</parent>");
createProjectSubFile("m2/src/io.properties", "");
importProject();
assertModules("project", "m1", "m2");
assertModuleLibDeps("m1");
assertModuleLibDeps("m2", "Maven: junit:junit:4.0");
}
public void testProjectWithProfilesXmlFile() throws Exception {
createProjectPom("<groupId>test</groupId>" +
"<artifactId>project</artifactId>" +