Skip to content

Commit

Permalink
Interpret profiles and property definitions from .mvn/maven.config wh…
Browse files Browse the repository at this point in the history
…en present
  • Loading branch information
jglick authored and matthiasblaesing committed May 27, 2019
1 parent ac716ae commit 5843ac5
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,12 @@ public Lookup getLookup() {
@Override
public File[] getFiles() {
File homeFile = FileUtil.normalizeFile(MavenCli.userMavenConfigurationHome);
return new File[] {new File(projectFile.getParentFile(), "nb-configuration.xml"), projectFile, new File(homeFile, "settings.xml")}; //NOI18N
return new File[] {
new File(projectFile.getParentFile(), "nb-configuration.xml"), //NOI18N
projectFile,
new File(new File(projectFile.getParentFile(), ".mvn"), "maven.config"), //NOI18N
new File(homeFile, "settings.xml"), //NOI18N
};
}
});
problemReporter = new ProblemReporterImpl(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@
package org.netbeans.modules.maven.modelcache;

import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
Expand Down Expand Up @@ -146,8 +150,27 @@ public static Collection<String> getUnknownBuildParticipantsClassNames(MavenProj
M2Configuration active = config.getActiveConfiguration();
MavenExecutionResult res = null;
try {
FileObject mavenConfig = projectDir.getFileObject(".mvn/maven.config");
List<String> mavenConfigOpts = Collections.emptyList();
if (mavenConfig != null && mavenConfig.isData()) {
mavenConfigOpts = Arrays.asList(mavenConfig.asText().split("\\s+"));
}
final MavenExecutionRequest req = projectEmbedder.createMavenExecutionRequest();
req.addActiveProfiles(active.getActivatedProfiles());
BiConsumer<String, String> addActiveProfiles = (opt, prefix) -> req.addActiveProfiles(Arrays.asList(opt.substring(prefix.length()).split(",")));
Iterator<String> optIt = mavenConfigOpts.iterator();
while (optIt.hasNext()) {
String opt = optIt.next();
// Could try to write/integrate a more general option parser here,
// but some options like -amd anyway violate GNU style.
if (opt.equals("-P") || opt.equals("--activate-profiles")) {
addActiveProfiles.accept(optIt.next(), "");
} else if (opt.startsWith("-P")) {
addActiveProfiles.accept(opt, "-P");
} else if (opt.startsWith("--activate-profiles=")) {
addActiveProfiles.accept(opt, "--activate-profiles=");
}
}

req.setPom(pomFile);
req.setNoSnapshotUpdates(true);
Expand All @@ -161,6 +184,26 @@ public static Collection<String> getUnknownBuildParticipantsClassNames(MavenProj
req.setOffline(true);
//#238800 important to merge, not replace
Properties uprops = req.getUserProperties();
BiConsumer<String, String> setProperty = (opt, prefix) -> {
String value = opt.substring(prefix.length());
int equals = value.indexOf('=');
if (equals != -1) {
uprops.setProperty(value.substring(0, equals), value.substring(equals + 1));
} else {
uprops.setProperty(value, "true");
}
};
optIt = mavenConfigOpts.iterator();
while (optIt.hasNext()) {
String opt = optIt.next();
if (opt.equals("-D") || opt.equals("--define")) {
setProperty.accept(optIt.next(), "");
} else if (opt.startsWith("-D")) {
setProperty.accept(opt, "-D");
} else if (opt.startsWith("--define=")) {
setProperty.accept(opt, "--define=");
}
}
uprops.putAll(createUserPropsForProjectLoading(active.getProperties()));
req.setUserProperties(uprops);
res = projectEmbedder.readProjectWithDependencies(req, true);
Expand Down Expand Up @@ -215,7 +258,7 @@ public static Collection<String> getUnknownBuildParticipantsClassNames(MavenProj
}
}
}
} catch (RuntimeException exc) {
} catch (RuntimeException | IOException exc) {
//guard against exceptions that are not processed by the embedder
//#136184 NumberFormatException
LOG.log(Level.INFO, "Runtime exception thrown while loading maven project at " + pomFile, exc); //NOI18N
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@

package org.netbeans.modules.maven;

import java.io.IOException;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import static junit.framework.TestCase.assertEquals;
import org.netbeans.api.annotations.common.SuppressWarnings;
import org.netbeans.api.java.queries.SourceLevelQuery;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectManager;
import org.netbeans.junit.NbTestCase;
Expand All @@ -44,8 +47,11 @@ public NbMavenProjectImplTest(String name) {
super(name);
}

private FileObject wd;

protected @Override void setUp() throws Exception {
clearWorkDir();
wd = FileUtil.toFileObject(getWorkDir());
//synchronous reload of maven project asserts sanoty in some tests..
System.setProperty("test.reload.sync", "true");
}
Expand All @@ -63,7 +69,7 @@ public void testPackagingTypeSpecificLookup() throws Exception {
assertLookupObject("[base, war]", "war");
assertLookupObject("[base]", "ear");
// Now test dynamic changes to packaging:
FileObject pd = FileUtil.toFileObject(getWorkDir()).getFileObject("prj-war");
FileObject pd = wd.getFileObject("prj-war");
Project prj = ProjectManager.getDefault().findProject(pd);
((NbMavenProjectImpl) prj).attachUpdater();
TestFileUtils.writeFile(pd, "pom.xml", "<project><modelVersion>4.0.0</modelVersion>"
Expand All @@ -72,7 +78,6 @@ public void testPackagingTypeSpecificLookup() throws Exception {
assertEquals("[base, jar]", prj.getLookup().lookup(I.class).m());
}
private void assertLookupObject(String result, String packaging) throws Exception {
FileObject wd = FileUtil.toFileObject(getWorkDir());
FileObject pd = wd.createFolder("prj-" + packaging);
TestFileUtils.writeFile(pd, "pom.xml", "<project><modelVersion>4.0.0</modelVersion>"
+ "<groupId>test</groupId><artifactId>prj-" + packaging + "</artifactId>"
Expand Down Expand Up @@ -122,7 +127,6 @@ public static class Merger implements LookupMerger<I> {
@RandomlyFails
public void testMemoryReleased() throws Exception {
TimedWeakReference.TIMEOUT = 0;
FileObject wd = FileUtil.toFileObject(getWorkDir());
TestFileUtils.writeFile(wd, "pom.xml", "<project><modelVersion>4.0.0</modelVersion>"
+ "<groupId>g</groupId><artifactId>a</artifactId>"
+ "<version>0</version></project>");
Expand All @@ -140,4 +144,56 @@ public void testMemoryReleased() throws Exception {
assertGC("can release project after updater detached", r, Collections.singleton(wd));
}

public void testMavenConfig() throws Exception {
TestFileUtils.writeFile(wd, "pom.xml", "<project><modelVersion>4.0.0</modelVersion>"
+ "<groupId>test</groupId><artifactId>prj</artifactId><version>1.0</version>"
+ "<properties><java>1.5</java><testJava>1.5</testJava></properties>"
+ "<build><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>2.1</version>"
+ "<configuration><source>${java}</source><testSource>${testJava}</testSource></configuration></plugin></plugins></build>"
+ "<profiles>"
+ "<profile><id>new</id><properties><java>1.6</java></properties></profile>"
+ "<profile><id>testNew</id><properties><testJava>1.6</testJava></properties></profile>"
+ "<profile><id>java8</id><activation><property><name>java8</name><value>true</value></property></activation><properties><java>1.8</java></properties></profile>"
+ "</profiles>"
+ "</project>");
FileObject source = TestFileUtils.writeFile(wd, "src/main/java/p/C.java", "package p; class C {}");
FileObject test = TestFileUtils.writeFile(wd, "src/test/java/p/CTest.java", "package p; class CTest {}");
((NbMavenProjectImpl) ProjectManager.getDefault().findProject(wd)).attachUpdater();
SourceLevelQuery.Result slqr = SourceLevelQuery.getSourceLevel2(source);
SourceLevelQuery.Result testSlqr = SourceLevelQuery.getSourceLevel2(test);
assertEquals("1.5", slqr.getSourceLevel());
assertEquals("1.5", testSlqr.getSourceLevel());
writeMavenConfig("-Pnew");
assertEquals("1.6", slqr.getSourceLevel());
writeMavenConfig("-P new");
assertEquals("1.6", slqr.getSourceLevel());
writeMavenConfig("--activate-profiles new");
assertEquals("1.6", slqr.getSourceLevel());
assertEquals("1.5", testSlqr.getSourceLevel());
writeMavenConfig("--activate-profiles=testNew");
assertEquals("1.5", slqr.getSourceLevel());
assertEquals("1.6", testSlqr.getSourceLevel());
writeMavenConfig("--activate-profiles new,testNew");
assertEquals("1.6", slqr.getSourceLevel());
assertEquals("1.6", testSlqr.getSourceLevel());
wd.getFileObject(".mvn/maven.config").delete();
assertEquals("1.5", slqr.getSourceLevel());
assertEquals("1.5", testSlqr.getSourceLevel());
writeMavenConfig("-Djava=1.7");
assertEquals("1.7", slqr.getSourceLevel());
writeMavenConfig("-D java=1.7");
assertEquals("1.7", slqr.getSourceLevel());
writeMavenConfig("--define java=1.6");
assertEquals("1.6", slqr.getSourceLevel());
writeMavenConfig("-Djava8");
assertEquals("1.8", slqr.getSourceLevel());
writeMavenConfig("-Djava8\n-PtestNew\n");
assertEquals("1.8", slqr.getSourceLevel());
assertEquals("1.6", testSlqr.getSourceLevel());
}
private void writeMavenConfig(String text) throws IOException, InterruptedException {
// Need the touch call, since NbMavenProjectImpl.Updater checks timestamps.
TestFileUtils.touch(TestFileUtils.writeFile(wd, ".mvn/maven.config", text), null);
}

}

0 comments on commit 5843ac5

Please sign in to comment.