简体   繁体   中英

Maven cannot find java.util.Objects when using mvn shade plugin?

I'm following AWS lambda's "create a jar using mvn":

http://docs.aws.amazon.com/lambda/latest/dg/java-create-jar-pkg-maven-no-ide.html

and some reason when I try to do either "mvn clean install" nor "mvn package" run properly. the project is using java 8.

Here's my 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>lambda</groupId>
    <artifactId>test</artifactId>
    <packaging>jar</packaging>
    <version>0.0.0.1-SNAPSHOT</version>
    <name>lambda-test</name>

    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-events</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.53</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

and my code:

package lambda.test;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.google.gson.Gson;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.util.Objects;
import java.util.Properties;
import java.util.Vector;

public class Test {

    private static final Gson gson = new Gson();

    public static String myHandler(Tester tester, Context context) {
        LambdaLogger logger = context.getLogger();
        logger.log("received: " + tester);

        try {
            JSch jSch = new JSch();
            Session session = jSch.getSession(username, sftpHost, sftpPort);
            session.setPassword(password);

            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");

            session.setConfig(config);
            session.connect(15000);

            ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();

            channel.cd("Inbox");
            Vector<ChannelSftp.LsEntry> list = channel.ls(".");

            logger.log("the list ==> \n" + gson.toJson(list));
        } catch (Exception e) {
            e.printStackTrace();
            logger.log("the exception: " + e.getMessage());
        }

        return String.valueOf(tester);
    }

    public static class Tester {
        private final String name;
        private final Integer id;

        public Tester(String name, Integer id) {
            this.name = name;
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public Integer getId() {
            return id;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Tester tester = (Tester) o;
            return  Objects.equals(name, tester.name) &&
                    Objects.equals(id, tester.id);
        }

        @Override
        public int hashCode() {
            return Objects.hash(name, id);
        }

        @Override
        public String toString() {
            return gson.toJson(this);
        }
    }
}

Maybe you must set the java -version of your project, and tell to the maven compiler the value of these setting like below :

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

I think it will working better right now! You can look at https://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html and other sections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM