Maven 给出错误:-source 1.5 中不支持使用资源进行试用

2022-09-03 16:46:16

当我尝试使用IntelliJ 12.1.4和Java 7使用Maven 3.0.5创建jar时遇到错误。我能够通过IDE运行项目,没有任何问题,但是当我尝试打包它时,我得到以下错误。

我的POM的相关部分(取自)是:Maven By ExampleSonatype

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>jar-with-dependencies</descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

错误是:

[ERROR] ...[33,55] error: diamond operator is not supported in -source 1.5
[ERROR] ...[207,7] error: try-with-resources is not supported in -source 1.5
[ERROR] ...[73,52] error: diamond operator is not supported in -source 1.5
[ERROR] ...[129,40] error: multi-catch statement is not supported in -source 1.5
[ERROR] ...[44,6] error: try-with-resources is not supported in -source 1.5
[ERROR] ...[28,39] error: diamond operator is not supported in -source 1.5
[ERROR] ...[31,7] error: try-with-resources is not supported in -source 1.5
[ERROR] ...[38,6] error: try-with-resources is not supported in -source 1.5
[ERROR] ...[34,41] error: diamond operator is not supported in -source 1.5
[ERROR] ...[77,43] error: diamond operator is not supported in -source 1.5
[ERROR] ...[84,6] error: try-with-resources is not supported in -source 1.5
[ERROR] ...[281,38] error: diamond operator is not supported in -source 1.5
[ERROR] ...[13,55] error: diamond operator is not supported in -source 1.5
[ERROR] ...[155,7] error: try-with-resources is not supported in -source 1.5

如何让 maven 使用源代码 1.7?


答案 1

要回答第一部分,请在POM中添加以下行以设置语言级别

 <properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>

添加这些行后,您可以成功构建jar,尽管当您运行它时,jar会给出错误。no main manifest attribute

这可以通过运行来修复,例如java -cp app.jar com.somepackage.SomeClass

或者要纠正这一点并制作一个可执行的jar,让你的pom看起来像

 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>fully.qualified.main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

这个pom通过将依赖项复制到构建目录,然后使用包含的库创建jar来克服描述符Ref的一些问题。jar-with-dependencies

感谢@André Aronsen的pom解决方案。

关于没有主要清单错误,有很多关于这个问题的帖子,有些解决方案有效,有些则不起作用。这个解决方案对我有用,所以我把它包括在这篇文章中完成。

使用Java 7,Maven 3.0.5和JetBrains IntelliJ IDEA 12.1.4 Ultimate进行了测试。


答案 2

推荐