Maven构建后Jar无.class文件,Target/Classes为空求助
Maven打包后target/classes为空,Jar无.class文件排查与修复
问题描述
执行mvn clean install后,target目录下的classes文件夹是空的,生成的Jar中没有包含编译后的.class文件。项目使用Groovy编写(1个Groovy脚本+1个AWS Lambda Handler Groovy类),代码存放于/src/main/java/路径下,已配置maven-assembly-plugin生成AWS Lambda所需的Zip包,相关配置如下:
pom.xml配置
<?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>com.example</groupId> <artifactId>dvalidator</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <maven-assembly-plugin.version>2.2.2</maven-assembly-plugin.version> <maven-resources-plugin.version>2.5</maven-resources-plugin.version> <maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.codehaus.gmaven/groovy-maven-plugin --> <dependency> <groupId>org.codehaus.gmaven</groupId> <artifactId>groovy-maven-plugin</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-lambda-java-core</artifactId> <version>1.2.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <id>assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/main/java/assembly/assembly.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>${maven-assembly-plugin.version}</version> <executions> <execution> <id>assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> </archive> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${maven-surefire-plugin.version}</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>${maven-resources-plugin.version}</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
assembly.xml配置
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id>bin</id> <formats> <format>zip</format> </formats> <fileSets> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets> <dependencySets> <dependencySet> <outputDirectory>lib/</outputDirectory> <useProjectArtifact>false</useProjectArtifact> <scope>runtime</scope> <excludes> </excludes> </dependencySet> </dependencySets> </assembly>
核心错误原因
- 未配置Groovy编译插件:默认的
maven-compiler-plugin仅处理.java文件,不会编译Groovy代码,导致Groovy文件未被转换成.class文件,target/classes自然为空。 - Groovy依赖错误:当前依赖的
groovy-maven-plugin是Maven插件,不是Groovy语言的核心库,项目编译时缺少Groovy运行时支持。 - 重复配置maven-assembly-plugin:pom中两次配置了maven-assembly-plugin,且执行ID相同,会导致打包逻辑冲突,可能覆盖正确的打包结果。
修复步骤
1. 添加Groovy核心依赖与编译插件
在<dependencies>中添加Groovy核心库,在<build><plugins>中添加Groovy编译插件,确保Groovy代码被正确编译:
<!-- Groovy核心依赖 --> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>3.0.9</version> <!-- 选择与JDK11兼容的版本 --> <scope>compile</scope> </dependency> <!-- Groovy编译插件,负责编译Groovy代码 --> <plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>groovy-maven-plugin</artifactId> <version>2.0</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin>
2. 移除重复的maven-assembly-plugin配置
保留自定义的Zip打包配置(适配AWS Lambda需求),删除重复的jar-with-dependencies配置,修改后的plugins部分如下:
<plugins> <!-- 自定义AWS Lambda Zip打包配置 --> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>${maven-assembly-plugin.version}</version> <executions> <execution> <id>assembly-zip</id> <!-- 修改ID避免冲突 --> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/main/java/assembly/assembly.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> <!-- 保留其他必要插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${maven-surefire-plugin.version}</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>${maven-resources-plugin.version}</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 添加Groovy编译插件 --> <plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>groovy-maven-plugin</artifactId> <version>2.0</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> </plugins>
3. 优化assembly.xml(可选)
如果希望将项目自身的.class文件直接打包到Zip根目录(而非依赖Jar),可以修改assembly.xml的<fileSets>,添加对编译输出目录的引用:
<fileSets> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> <!-- 加入项目编译后的class文件 --> <fileSet> <directory>${project.build.outputDirectory}</directory> <outputDirectory>/</outputDirectory> </fileSet> </fileSets>
完成以上修改后,重新执行mvn clean install,target/classes会生成Groovy编译后的.class文件,最终生成的Zip包也会包含必要的代码与依赖。
内容的提问来源于stack exchange,提问作者deewreck
相关产品推荐
相关产品推荐

