Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review: fix NPE in MavenLauncher when there is no build section #1720

Merged
merged 2 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/main/java/spoon/MavenLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,18 @@ private String getProperty(String key) {
* @return the source version of the project
*/
public int getSourceVersion() {
for (Plugin plugin : model.getBuild().getPlugins()) {
if (!"maven-compiler-plugin".equals(plugin.getArtifactId())) {
continue;
}
Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
Xpp3Dom source = configuration.getChild("source");
if (source != null) {
return Integer.parseInt(extractVariable(source.getValue()).substring(2));
if (model.getBuild() != null) {
for (Plugin plugin : model.getBuild().getPlugins()) {
if (!"maven-compiler-plugin".equals(plugin.getArtifactId())) {
continue;
}
Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
Xpp3Dom source = configuration.getChild("source");
if (source != null) {
return Integer.parseInt(extractVariable(source.getValue()).substring(2));
}
break;
}
break;
}
String javaVersion = getProperty("java.version");
if (javaVersion != null) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/spoon/MavenLauncherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ public void mavenLauncherOnANotExistingFileTest() {
public void mavenLauncherOnDirectoryWithoutPomTest() {
new MavenLauncher("./src", MavenLauncher.SOURCE_TYPE.APP_SOURCE);
}

@Test
public void mavenLauncherTestWithVerySimpleProject() {
MavenLauncher launcher = new MavenLauncher("./src/test/resources/maven-launcher/very-simple", MavenLauncher.SOURCE_TYPE.ALL_SOURCE);
assertEquals(1, launcher.getModelBuilder().getInputSources().size());
}
}
10 changes: 10 additions & 0 deletions src/test/resources/maven-launcher/very-simple/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.foo.bar</groupId>
<artifactId>foobar</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>foobar</name>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class Test {
int field = 42;
}