【问题标题】:What's wrong with how I am using maven to build natty?我如何使用 maven 构建 natty 有什么问题?
【发布时间】:2015-03-03 05:24:43
【问题描述】:

我正在尝试通过将 NATTY 包含为 maven 依赖项来使用它。我刚刚做了 Hello, World Maven tutorial——但我对 Maven 不熟悉。 natty 网站上的说明说将 natty 作为依赖项包含在 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>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency> 
      <groupId>com.joestelmach</groupId>
      <artifactId>natty</artifactId>
      <version>0.9</version>
    </dependency>
  </dependencies>
</project>

然后我运行$mvn package 并且项目成功构建。我在我的 /target: my-app-1.0-SNAPSHOT.jar 中看到一个 jar 文件,所以我假设 natty 依赖项被烘焙到该 jar 中。

为了测试,我在一个名为 Temporary.java 的文件中创建了一个简单的类来保存简洁的演示代码:

import com.joestelmach.natty.*;
import com.joestelmach.natty.generated.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class Temporary{
    public static void main(String [] args) {
        Parser parser = new Parser();
        List groups = parser.parse("the day before next thursday");
        for (DateGroup group : groups) {
            List dates = group.getDates();
            int line = group.getLine();
            int column = group.getPosition();
            String matchingValue = group.getText();
            String syntaxTree = group.getSyntaxTree().toStringTree();
            Map parseMap = group.getParseLocations();
            boolean isRecurreing = group.isRecurring();
            Date recursUntil = group.getRecursUntil();
        }
    }
}

但是当我运行$ javac -cp target/my-app-1.0-SNAPSHOT.jar Temporary.java 时,我得到了

Temporary.java:1: error: package com.joestelmach.natty does not exist
import com.joestelmach.natty.*;

我做错了什么?

【问题讨论】:

  • 首先,对问题标题的挑剔:你没有建立整洁。您正在构建其他依赖于 natty 的东西。其次,为了详细说明@medium 的答案,您构建的 jar 仍然依赖于 natty - 这意味着 natty.jar 需要在您的类路径中。您的 jar 不会自动包含 natty.jar。有多种方法可以解决此问题; medium 显示了一个将所有 natty 的类合并到你的 jar 中的类,这可能是可取的,也可能不是可取的。

标签: java maven natty


【解决方案1】:

您需要确保在使用 maven 打包 jar 时包含您的依赖项。

我相信您需要将其添加到您的 pom.xml 中

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
      <manifest>
        <mainClass></mainClass>
      </manifest>
    </archive>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

确保在运行它时使用末尾带有“jar-with-dependencies.jar”的 .jar。这将确保包含您的 natty 依赖项。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-27
  • 2010-09-27
相关资源
最近更新 更多