bernie2436
bernie2436

Reputation: 23911

What's wrong with how I am using maven to build natty?

I am trying to use NATTY by including it as a maven dependency. I just did the Hello, World Maven tutorial -- but am otherwise unfamiliar with Maven. The instructions on the natty site say to include natty as a dependency in the pom.xml. I have done so like this

<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>

I then run $mvn package and the project builds successfully. I see one jar file in my /target: my-app-1.0-SNAPSHOT.jar so I assume that the natty dependencies are baked into that jar.

To test, I create a simple class in a file called Temporary.java to hold the natty demo code:

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();
        }
    }
}

But when I run $ javac -cp target/my-app-1.0-SNAPSHOT.jar Temporary.java I get

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

What am I doing wrong?

Upvotes: 2

Views: 471

Answers (1)

medium
medium

Reputation: 4236

You need to make sure that when you package your jar using maven that your dependencies are included.

I believe you need to add this to your 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>

Make sure when running it you use the .jar that has the "jar-with-dependencies.jar" at the end. This will ensure that your natty dependency is included.

Upvotes: 1

Related Questions