【问题标题】:Java class not compiling working outside of netbeans with maven dependiciesJava 类不编译在具有 Maven 依赖项的 netbeans 之外工作
【发布时间】:2015-08-08 23:48:22
【问题描述】:

我已经用 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,所以我对此很陌生。

【问题讨论】:

  • 您要么需要将依赖项“捆绑”在生成的 jar 中,要么复制到生成的 jar 可以从中找到它们的特定位置。

标签: java maven netbeans netbeans-8


【解决方案1】:

您的 pom 文件中有 maven-shade-plugin,它会创建一个可执行的“胖”jar,并合并所有依赖项。所以在你执行了 maven cleanpackage 目标之后,你应该像这样从命令行运行这个 jar:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 2015-12-16
    • 2014-03-22
    • 2015-09-10
    相关资源
    最近更新 更多