15

I am getting errors when trying to create a jar with Maven 3.0.5 using IntelliJ 12.1.4 and Java 7. I am able to run the project via the IDE with no problems, but when I try to package it I get the following errors.

The relevant section of my POM (taken from Maven By Example by Sonatype) is:

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

and the errors are:

[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

How can I get maven to use source 1.7?

Robert H
  • 11,520
  • 18
  • 68
  • 110
  • Maven doesn't give this error. The Java compiler does. – user207421 Jun 25 '13 at 22:35
  • @EJP The issue only manifested itself when compiling with Maven, not through IDEA, which is why I suspected Maven. Adding the source level properties to the POM resolved that error, which would also point to Maven being the source of the issue. That would make the compiler the symptom, not the cause. – Robert H Jun 26 '13 at 12:22
  • it seems totally bizaare that 1.5 (or is that 5!) is the default and not 7 when the compiler is version 7. Not a lot of confidence in their new versions maybe? – JonnyRaa May 13 '14 at 08:29

1 Answers1

47

To answer the first part, add the following lines to the POM to set language level

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

After adding those lines you can successfully build your jar, although when you run it your jar will give a no main manifest attribute error.

This can either be fixed by running like java -cp app.jar com.somepackage.SomeClass

or to correct this and make an executable jar, make your pom look like

 <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>

This pom overcomes some issues with the jar-with-dependencies descriptorRef by copying dependencies to a build dir, and then creating the jar with the included libraries.

Thanks goes out to @André Aronsen for his pom solution.

Regarding the no main manifest error, there are a lot of posts about this issue, some solutions work, some don't. This solution works for me, so I've included it in this post for completion.

Tested with Java 7, Maven 3.0.5 and JetBrains IntelliJ IDEA 12.1.4 Ultimate.

Community
  • 1
  • 1
Robert H
  • 11,520
  • 18
  • 68
  • 110