Maven编译失败求助:maven-compiler-plugin:3.7.0执行报错
First, let's recap your issue clearly:
You're hitting a generic compilation failure when building your Maven project:
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project MySampleProj: Compilation failure: Compilation failure:
Here's your provided pom.xml (note it appears to be incomplete at the end):
<?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>MySampleProj</groupId> <artifactId>MySampleProj</artifactId> <version>0.0.2-SNAPSHOT</version> <name>MySampleProj</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <maven-compiler-plugin-version>3.7.0</maven-compiler-plugin-version> <cxf.version>3.1.6</cxf.version> <jra.version>2.1.16</jra.version> <jdk.version>1.7</jdk.version> <jaxws.version>2.0.1</jaxws.version> <spring.version>5.0.6.RELEASE</spring.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.0.6.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5.5</version> <scope>compile</scope> </dependency> </dependencies> </dependencyManagement> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin-version}</version> <configuration> <verbose>true</verbose> <fork>true</fork> <source>${jdk.version}</source> <target>${jdk.version}</target> <compilerVersion>${java.version}</compilerVersion> <executable>${java.home}/bin/javac</executable> </configuration> </plugin> <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <release>8</release> <verbose>true</verbose> </configuration> </plugin> --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>
Let's walk through the key issues in your setup and how to fix them:
1. Spring 5.0.6 is incompatible with JDK 1.7
This is the biggest red flag. Spring Framework 5.x requires Java 8 or higher—it doesn't support JDK 1.7 at all. Using a Spring 5 version with JDK 1.7 will throw compilation errors because Spring uses Java 8 features like lambdas, default methods, and newer APIs that don't exist in Java 7.
Fix Options:
- Upgrade to JDK 8: Update your properties to target Java 8:
Make sure your local Maven environment is also using JDK 8 (check with<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <jdk.version>1.8</jdk.version>java -version). - Downgrade Spring: If you must use JDK 1.7, switch to Spring 4.3.x (the last Spring version supporting Java 7):
<spring.version>4.3.30.RELEASE</spring.version>
2. Undefined ${java.version} property
In your maven-compiler-plugin configuration, you're referencing ${java.version} for <compilerVersion>, but you never defined this property in your <properties> section. Maven can't resolve this variable, which breaks the compiler plugin setup.
Fix:
Either add the property to your <properties>:
<java.version>1.8</java.version> <!-- Match your JDK version -->
Or replace ${java.version} with your existing ${jdk.version} property:
<compilerVersion>${jdk.version}</compilerVersion>
3. Duplicate maven-compiler-plugin entries
You have two separate entries for maven-compiler-plugin in your <pluginManagement> section: one using the ${maven-compiler-plugin-version} property, and another hardcoding version 3.7.0. This creates ambiguity and can cause Maven to pick the wrong configuration.
Fix:
Remove the duplicate entry. Keep the one with the property-based version (it's more maintainable):
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin-version}</version> <configuration> <verbose>true</verbose> <fork>true</fork> <source>${jdk.version}</source> <target>${jdk.version}</target> <compilerVersion>${jdk.version}</compilerVersion> <!-- Fixed property here --> <executable>${java.home}/bin/javac</executable> </configuration> </plugin>
4. Incomplete pom.xml
Your provided pom.xml cuts off mid-way through the maven-jar-plugin entry. A malformed XML file will cause Maven to fail immediately. Make sure you close all open tags:
</plugin>for the jar plugin</plugins></pluginManagement></build></project>
Bonus Debug Tip
Run Maven with debug logging to get the exact compilation errors (the generic "Compilation failure" message doesn't tell you much):
mvn clean compile -X
This will show you specific issues like missing classes, incompatible method calls, or syntax errors that are causing the failure.
After applying these fixes, try running mvn clean compile again—this should resolve the compilation issues.
内容的提问来源于stack exchange,提问作者Prasad

