从命令行运行Verticle时找不到软件包

时间:2018-06-21 09:42:23

标签: java maven vert.x

我正在使用MavenVert.x创建一个应用程序。 当我在IDE(IntelliJ)中运行它时,一切工作都很好,但是我无法使用命令行来使其工作。

我有一个Launcher类,该类部署了一些顶点,但是所有的顶点都存在相同的问题。

到目前为止,这是我尝试过的:

vertx run Launcher.java
vertx run com.packagename.Launcher.java
// user-content-service-0.1.jar create via: mvn clean package
run com.packagename.Launcher.java -cp target\user-content-service-0.1.jar

作为错误,我得到了:

.../path/Launcher.java:8: error: cannot find symbol
private static final Logger logger = logManager.getLogger(Launcher.class);

symbol: variable LogManager
location: class com.packagename.Launcher
java.lang.RuntimeException: Compilation failed
...

问题似乎来自于编译器无法找到依赖项的事实

这是我的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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.packagename</groupId>
  <artifactId>user-content-service</artifactId>
  <packaging>jar</packaging>
  <version>0.1</version>
  <name>Project - user-content-service</name>
  <url>http://maven.apache.org</url>

  <properties>
    <vertx.version>[3.5.0,3.6)</vertx.version>
    <java.version>1.8</java.version>
    <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
    <log4j.version>[2.10.0,2.11)</log4j.version>
    <junit.version>4.12</junit.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-core</artifactId>
      <version>${vertx.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-slf4j-impl</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-unit</artifactId>
      <version>${vertx.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-dynamodb</artifactId>
      <version>LATEST</version>
    </dependency>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-config</artifactId>
      <version>${vertx.version}</version>
    </dependency>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-web</artifactId>
      <version>${vertx.version}</version>
    </dependency>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-web-client</artifactId>
      <version>${vertx.version}</version>
    </dependency>
    <dependency>
      <groupId>guru.nidi.raml</groupId>
      <artifactId>raml-tester</artifactId>
      <version>0.9.1</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven-compiler-plugin.version}</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>   

这是我的Launcher.java文件:

package com.packagename;

import io.vertx.core.AbstractVerticle; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger;

public class Launcher extends AbstractVerticle {
    private static final Logger logger = LogManager.getLogger(Launcher.class);

    @Override
    public void start() {
        vertx.deployVerticle("com.packagename.DynamoDBVerticle", logRes -> {
            if (logRes.succeeded()) {
                vertx.deployVerticle("com.packagename.UploaderVerticle", uploaderRes -> {
                    if (uploaderRes.succeeded()) {
                        vertx.deployVerticle("com.packagename.ServerVerticle", serverRes -> {
                            if (!serverRes.succeeded()) {
                                logger.error("Could not start server");
                            }
                        });
                    }
                    else {
                        logger.error("Could not start uploader");
                    }
                });
            }
            else {
                logger.error("Could not start Dynamo");
            }
        });
    } }

任何线索,我在这里可能做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:2)

您遇到类路径问题;因为在打包阶段将jarfile与maven-jar打包在一起,所以只能在build目录中获取它。如果要使用java -jar在命令行上运行,则应创建一个fat-jar(又名uber-jar,一个专门打包的jar文件,以便包含您的类和在其中声明的所有依赖项。 pom文件)。

您可以在打包阶段添加maven-shade-plugin来完成此操作,即:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>2.3</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <transformers>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <manifestEntries>
                <Main-Class>io.vertx.core.Launcher</Main-Class>
                <Main-Verticle>com.packagename.Launcher</Main-Verticle>
            </manifestEntries>
          </transformer>
          <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource>
          </transformer>
        </transformers>
        <artifactSet/>
        <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
      </configuration>
    </execution>
  </executions>
</plugin>

注意:您在vert.x Launcher(在胖子罐中用作Main-Class)和您自己的Launcher(实际上是Verticle)之间存在类名冲突:我建议将其重命名

maven package之后,您将可以执行以下操作:

java -jar target/user-content-service-0.1-fat.jar 

签入vert.x samples以获得更多信息。