使用mvn shade插件时,Maven找不到java.util.Objects?

时间:2015-10-15 18:45:39

标签: java maven amazon-web-services lambda

我跟随AWS lambda"使用mvn创建一个jar":

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

当我尝试做任何一次" mvn clean install"也不是" mvn package"运行正常。该项目正在使用java 8。

这是我的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>

和我的代码:

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);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

也许您必须设置项目的java -version,并告诉maven编译器这些设置的值如下所示:

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

我认为它现在会更好用! 您可以查看https://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html和其他部分。