0

I have downloaded maven through apt-get, I have maven integration installed on Eclipse. I have pom.xml with spark dependency set. My project compiles and when I hit run I receive this output:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building mc437-web 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.105s
[INFO] Finished at: Thu Apr 10 10:56:15 PDT 2014
[INFO] Final Memory: 6M/59M
[INFO] ------------------------------------------------------------------------

What do I need to have my app running on localhost? What goal/profile I have to set on Maven?

Yuri A. Brito
  • 231
  • 2
  • 10

3 Answers3

1

You need a class with a main method and which defines some routes. Then you can right click and run that class or run it from the command line.

public class App 
{
    public static void main(String[] args) {

      get(new Route("/hello") {
         @Override
         public Object handle(Request request, Response response) {
            return "Hello World!";
         }
      });
}
Shekhar
  • 109
  • 3
0

If you want to build your Spark project with Maven including all dependencies (for example, build one jar file), you can use Apache Maven Assembly Plugin. So, you can add to pom.xml such plugin definition:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>attached</goal>
      </goals>
      <phase>package</phase>
      <configuration>
        <finalName>%JAR_FILE_NAME%</finalName>
        <descriptorRefs>
          <descriptorRef>%JAR_FILE_NAME_DESCIPTOR%</descriptorRef>
        </descriptorRefs>
        <archive>
          <manifest>
            <mainClass>%FULL_MAIN_CLASS_NAME%</mainClass>
          </manifest>
        </archive>
      </configuration>
    </execution>
  </executions>
</plugin>

And for running you project you can just run jar:

java -jar %JAR_NAME%

As for integration with Eclipse - you can use mvn eclipse:eclipse in command line, or use Maven plugin for Eclipse, and chose in menu "File -> Import". See more information in this question.

Community
  • 1
  • 1
Hleb
  • 7,037
  • 12
  • 58
  • 117
0

Using SparkJava v2.5.5, the code looks like this:

public class App {
    public static void main(String[] args) {

       Spark.get("/hello", new Route() {
           public Object handle(Request request, Response response) {
               return "Hello World!";
           }
       });
}

To test, run the java application and call in your browser http://localhost:4567/hello

Rogério Arantes
  • 712
  • 1
  • 8
  • 29