Scala akka - 无法从Maven构建的jar依赖项运行程序

时间:2014-05-27 20:31:21

标签: eclipse scala maven jar akka

我是scala和akka的新手。我试图用客户端和服务器设置简单的akka​​远程项目。我正在使用带有maven scala插件的Eclipse。如果我从IDE运行这个项目,一切正常。客户端能够连接到服务器。不幸的是,当我使用maven-assembly-plugin将项目构建为jar-with-dependency时,我无法从命令行运行它。我收到这样的错误:

  

F:\ Projects \ sag-project-scala \ sag-scala \ target> scala sag-scala-1.0-jar-with-dependencies.jar服务器   com.typesafe.config.ConfigException $ Missing:找不到密钥'akka.loggers'的配置设置           在com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:124)           在com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:145)           在com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:151)           在com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:159)           在com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:164)           在com.typesafe.config.impl.SimpleConfig.getList(SimpleConfig.java:212)           at com.typesafe.config.impl.SimpleConfig.getHomogeneousUnwrappedList(SimpleConfig.java:271)           at com.typesafe.config.impl.SimpleConfig.getStringList(SimpleConfig.java:329)           at akka.actor.ActorSystem $ Settings。(ActorSystem.scala:179)           at akka.actor.ActorSystemImpl。(ActorSystem.scala:504)           at akka.actor.ActorSystem $ .apply(ActorSystem.scala:141)           at akka.actor.ActorSystem $ .apply(ActorSystem.scala:118)           at com.sag.remote.ServerObject $ .run(Server.scala:28)           at com.sag.remote.ServerObject $ .main(Server.scala:25)           在com.sag.remote.ServerObject.main(Server.scala)           在com.sag.main.ScalaRunner.main(ScalaRunner.java:23)           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)           at java.lang.reflect.Method.invoke(Unknown Source)           在scala.tools.nsc.util.ScalaClassLoader $$ anonfun $ run $ 1.apply(ScalaClassLoader.scala:71)           在scala.tools.nsc.util.ScalaClassLoader $ class.asContext(ScalaClassLoader.scala:31)           在scala.tools.nsc.util.ScalaClassLoader $ URLClassLoader.asContext(ScalaClassLoader.scala:139)           在scala.tools.nsc.util.ScalaClassLoader $ class.run(ScalaClassLoader.scala:71)           在scala.tools.nsc.util.ScalaClassLoader $ URLClassLoader.run(ScalaClassLoader.scala:139)           在scala.tools.nsc.CommonRunner $ class.run(ObjectRunner.scala:28)           在scala.tools.nsc.JarRunner $ .run(MainGenericRunner.scala:16)           在scala.tools.nsc.CommonRunner $ class.runAndCatch(ObjectRunner.scala:35)           在scala.tools.nsc.JarRunner $ .runJar(MainGenericRunner.scala:28)           在scala.tools.nsc.MainGenericRunner.runTarget $ 1(MainGenericRunner.scala:78)           在scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)           在scala.tools.nsc.MainGenericRunner $ .main(MainGenericRunner.scala:105)           在scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

如果我理解akka文档,在自定义* .conf文件中我会覆盖默认配置,而akka会在内部将这些文件解析为一个文件。 我很确定,我错过了一些非常小的东西但是两天后谷歌搜索我放弃了。这就是我要求你帮忙的原因。 好像它无法从application.conf找到正确的配置,但为什么从IDE一切正常? 以下是用于构建我的示例的文件:

ScalaRunner.java

package com.sag.main;

import com.sag.remote.ClientObject;
import com.sag.remote.ServerObject;

/**
 * Runner class to simply run jar file from command line.
 * @author ddr
 *
 */
public class ScalaRunner {
    private static String SERVER_MODE = "server";
    private static String CLIENT_MODE = "client";
    public static void main(String[] args) {
        String mode = SERVER_MODE;
        if(args != null && args.length > 0){
            mode = args[0];
        }
        if(CLIENT_MODE.equals(mode)){
            ClientObject.main(args);
        }
        else if (SERVER_MODE.equals(mode)){
            ServerObject.main(args);
        }
    }
}

Client.scala

package com.sag.remote

import akka.actor._
import akka.actor.ActorDSL._
import com.typesafe.config.ConfigFactory

class Client extends Actor {
  def receive = {
    case msg: String => println("joe received " + msg + " from " + sender)
    case _ => println("Received unknown msg ")
  }
}

object ClientObject  {
    def main(args: Array[String]): Unit = run()

  def run() = {
     println("STARTING CLIENT")
     implicit val client = ActorSystem("Client", ConfigFactory.load("client"))
     val server = client.actorFor("akka.tcp://server@127.0.0.1:6969/user/server")
     println("That 's remote server:" + server)
     server ! "Hello"
  }
}

Server.scala

package com.sag.remote

import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
import com.typesafe.config.Config
import scala.concurrent.duration.Duration
import java.util.concurrent.TimeUnit
import akka.actor.Extension
import akka.actor.ExtensionIdProvider
import akka.actor.ExtensionId
import akka.actor.ExtendedActorSystem
import akka.actor.ActorSystem.Settings
import com.typesafe.config.ConfigFactory


class Server extends Actor {
  def receive = {
    case msg: String => println("joe received " + msg + " from " + sender)
    case _ => println("Received unknown msg ")
  }
}

object ServerObject { 
  def main(args: Array[String]): Unit = run()

  def run() = {
    val server = ActorSystem("server", ConfigFactory.load("server"))
    val serverActor = server.actorOf(Props[Server], name = "server")
    println(serverActor.path)
    println()
    println("Server ready")
  }
}

common.conf

include "application.conf"
akka{
    stdout-loglevel = "DEBUG"
    loglevel = "DEBUG"
    actor {
        provider = "akka.remote.RemoteActorRefProvider"
    }
    remote {
        enabled-transports = ["akka.remote.netty.tcp"]
        netty.tcp {
            hostname = "127.0.0.1"
        }
    }
}

client.conf

include "common"

akka {
  remote.netty.tcp.port = 2552
}

server.conf中

include "common"

akka {
  remote.netty.tcp.port = 6969
}

的pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sag-scala</groupId>
    <artifactId>sag-scala</artifactId>
    <version>1.0</version>
    <inceptionYear>2008</inceptionYear>
    <properties>
        <scala.version>2.10.1</scala.version>
    </properties>

    <repositories>
        <repository>
            <id>scala-tools.org</id>
            <name>Scala-Tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </repository>
        <repository>
            <id>akka-snapshots</id>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <url>http://repo.akka.io/snapshots/</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>scala-tools.org</id>
            <name>Scala-Tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-remote_2.10</artifactId>
            <version>2.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor_2.10</artifactId>
            <version>2.3.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.specs</groupId>
            <artifactId>specs</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <buildcommands>
                        <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>
                    </buildcommands>
                    <additionalProjectnatures>
                        <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>
                    </additionalProjectnatures>
                    <classpathContainers>
                        <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
                        <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>
                    </classpathContainers>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.sag.main.ScalaRunner</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.sag.main.ScalaRunner</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <configuration>
                    <scalaVersion>${scala.version}</scalaVersion>
                </configuration>
            </plugin>
        </plugins>
    </reporting>
</project>

我将不胜感激任何帮助。 问候, 达留

1 个答案:

答案 0 :(得分:2)

这是修改后的pom.xml,解决了我的问题。也许它会帮助别人。

的pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sag-scala</groupId>
    <artifactId>sag-scala</artifactId>
    <version>1.0</version>
    <inceptionYear>2008</inceptionYear>
    <properties>
        <scala.version>2.10.1</scala.version>
    </properties>

    <repositories>
        <repository>
            <id>scala-tools.org</id>
            <name>Scala-Tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </repository>
        <repository>
            <id>akka-snapshots</id>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <url>http://repo.akka.io/snapshots/</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>scala-tools.org</id>
            <name>Scala-Tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-remote_2.10</artifactId>
            <version>2.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor_2.10</artifactId>
            <version>2.3.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.specs</groupId>
            <artifactId>specs</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
        <plugins>
            <!-- plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> 
                <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> 
                </goals> </execution> </executions> <configuration> <scalaVersion>${scala.version}</scalaVersion> 
                <args> <arg>-target:jvm-1.5</arg> </args> </configuration> </plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <buildcommands>
                        <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>
                    </buildcommands>
                    <additionalProjectnatures>
                        <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>
                    </additionalProjectnatures>
                    <classpathContainers>
                        <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
                        <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>
                    </classpathContainers>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <transformers>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                            <resource>reference.conf</resource>
                        </transformer>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <manifestEntries>
                                <Main-Class>com.sag.main.ScalaRunner</Main-Class>
                            </manifestEntries>
                        </transformer>
                    </transformers>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.sag.main.ScalaRunner</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <configuration>
                    <scalaVersion>${scala.version}</scalaVersion>
                </configuration>
            </plugin>
        </plugins>
    </reporting>
</project>