Java 类无法在具有 Maven 依赖项的 Netbeans 之外编译工作

标签 java maven netbeans netbeans-8

我已经用 Java 创建了一个项目。我添加了一个 Maven 依赖项 nimbus-jose-jwt。

我的 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>com.mycompany</groupId>
  <artifactId>vertxTestProject</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <vertx.version>3.0.0-milestone4</vertx.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-core</artifactId>
      <version>${vertx.version}</version>
    </dependency>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-apex</artifactId>
      <version>${vertx.version}</version>
    </dependency>
    <!-- Uncomment if you want to enable clustering with Hazelcast
    <dependency>
        <groupId>io.vertx</groupId>
        <artifactId>vertx-hazelcast</artifactId>
        <version>${vertx.version}</version>
    </dependency>-->
    <!-- Uncomment if you want to use the async database service
    <dependency>
        <groupId>io.vertx</groupId>
        <artifactId>vertx-mysql-postgresql-service</artifactId>
        <version>${vertx.version}</version>
    </dependency>-->
    <!-- Uncomment if you want to enable async mail sending service
    <dependency>
        <groupId>io.vertx</groupId>
        <artifactId>vertx-mail-service</artifactId>
        <version>${vertx.version}</version>
    </dependency>-->
    <!-- Uncomment if you want to enable reactive streams
    <dependency>
        <groupId>io.vertx</groupId>
        <artifactId>vertx-reactive-streams</artifactId>
        <version>${vertx.version}</version>
    </dependency>-->
    <!-- Uncomment if you want to enable mongo DB service
    <dependency>
        <groupId>io.vertx</groupId>
        <artifactId>vertx-mongo-service</artifactId>
        <version>${vertx.version}</version>
    </dependency>-->
    <!-- Uncomment if you want to enable metrics
    <dependency>
        <groupId>io.vertx</groupId>
        <artifactId>vertx-dropwizard-metrics</artifactId>
        <version>${vertx.version}</version>
    </dependency>-->
    <!-- Uncomment if you want to enable the JDBC database service
    <dependency>
        <groupId>io.vertx</groupId>
        <artifactId>vertx-jdbc-service</artifactId>
        <version>${vertx.version}</version>
    </dependency>-->
    <!-- Uncomment if you want to enable the auth service
    <dependency>
        <groupId>io.vertx</groupId>
        <artifactId>vertx-auth-service</artifactId>
        <version>${vertx.version}</version>
    </dependency>-->
    <!-- Uncomment if you want to use the RxJava API for Vert.x
    <dependency>
        <groupId>io.vertx</groupId>
        <artifactId>vertx-rx-java</artifactId>
        <version>${vertx.version}</version>
    </dependency>-->
    <dependency>
      <groupId>com.restfb</groupId>
      <artifactId>restfb</artifactId>
      <version>1.10.1</version>
    </dependency>
    <dependency>
      <groupId>com.nimbusds</groupId>
      <artifactId>nimbus-jose-jwt</artifactId>
      <version>3.10</version>
      <type>jar</type>
    </dependency>
  </dependencies>

  <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>
      <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.Starter</Main-Class>
                    <Main-Verticle>com.mycompany.vertxtestproject.Main</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></artifactSet>
              <outputFile>${project.build.directory}/vertxTestProject-${project.version}-fat.jar</outputFile>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.3.2</version>
        <executions>
          <execution>
            <goals>
              <goal>exec</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>io.vertx.core.Starter</mainClass>
          <additionalClasspathElements>
            <additionalClasspathElement>${basedir}/src/main/java</additionalClasspathElement>
          </additionalClasspathElements>
          <systemProperties>
            <systemProperty>
              <key>vertx.deployment.options.redeploy</key>
              <value>true</value>
            </systemProperty>
            <systemProperty>
              <key>vertx.deployment.options.redeployScanPeriod</key>
              <value>100</value>
            </systemProperty>
          </systemProperties>
          <arguments>
            <argument>run</argument>
            <argument>com/mycompany/vertxtestproject/Main.java</argument>
            <!--                  <argument>-cluster</argument>
            <argument>-cluster-host</argument>
            <argument>127.0.0.1</argument>-->
          </arguments>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

当我在 netbeans 内部运行时,所有内容都会编译并运行。当我清理和构建并尝试通过命令行运行时,我收到以下错误:

error: package com.nimbusds.jose does not exist

这是我第一次使用maven,所以我对此很陌生。

最佳答案

您的 pom 文件中有 maven-shade-plugin,它会创建一个可执行的“fat”jar,其中合并了所有依赖项。因此,在执行 maven cleanpackage 目标后,您应该从命令行运行这个 jar,如下所示:

java -jar target/vertxTestProject-1.0-SNAPSHOT-fat.jar

关于Java 类无法在具有 Maven 依赖项的 Netbeans 之外编译工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30473195/

相关文章:

java - 打印 Arraylist 中的对象 - 线程 "main"java.lang.IndexOutOfBoundsException : Index: 10, 中出现异常大小:10

java - 如何获取java对象在内存堆中占用的空间?

java - mvn package命令抛出 "error 401 Unauthorized "

Java Netbeans 覆盖 JPanel 中的 paint() 方法

java - 将 mongo 实体保存到不同的集合中

java - 检查多个整数是否大于一个整数

ant - 是否有与mvn jar-with-dependencies等同的 Ivy / Ant ?

java - Spring JOOQ 生成部分失败

mysql - 多重定义错误

java - BorderLayout 隐藏 JPane