Help with maven and spark - [WARNING] Unused declared dependencies

717 views
Skip to first unread message

Luiz Carlos da Silveira Júnior

unread,
Sep 29, 2014, 9:58:48 PM9/29/14
to spar...@googlegroups.com
Can help me with this hello?

#mvn --version
Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T20:58:10+00:00)
...

#mvn dependency:analyze
...
[WARNING] Unused declared dependencies found:
[WARNING]    com.sparkjava:spark-core:jar:2.0.0:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
...

#mvn javadoc:javadoc
...
[WARNING] /tmp/my-app/src/main/java/com/mycompany/app/App.java:2: error: package spark does not exist [WARNING] import static spark.Spark.*;
...
[INFO] BUILD SUCCESS
...

#mvn package
...
[INFO] BUILD SUCCESS
...

#java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
 Exception in thread "main" java.lang.NoClassDefFoundError: spark/Request


#vi pom.xml
    <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.sparkjava</groupId>
            <artifactId>spark-core</artifactId>
            <version>2.0.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


John Watson

unread,
Sep 29, 2014, 10:58:51 PM9/29/14
to Luiz Carlos da Silveira Júnior, spar...@googlegroups.com
Hi, Luiz,

Maven doesn't, by default, include all the jars inside the jar for your project. You'll need to have a classpath for your java command that also includes the spark jar.  Or, you'll need to use the appassembler plugin to have it build a runnable application for you.

Hope that helps,

John


--
You received this message because you are subscribed to the Google Groups "sparkjava" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sparkjava+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Markus Mahlberg

unread,
Oct 1, 2014, 3:18:57 AM10/1/14
to spar...@googlegroups.com
Another option other than John Watson's suggestion of app assembler would be to use the maven assembly plugin, which is quite easy to use:

  <build>

   <plugins>

     <plugin>

       <artifactId>maven-assembly-plugin</artifactId>

         <configuration>

           <archive>

             <manifest>
               <mainClass>fully.qualified.main.Class</mainClass>

             </manifest>
            </archive>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>

          </configuration>
        </plugin>

      </plugins>

   </build>


which you would have to call with maven clean compile assembly:single in order to build a "fat jar" with all dependencies.

Of course, you can bind the plugin to any phase (I'd suggest "package") for automatic creation of the fat jar.

Kind regards,

Markus

a platypus

unread,
Oct 1, 2014, 7:45:29 PM10/1/14
to spar...@googlegroups.com
Hi Luiz,

Just to expand on Markus and John's suggestions.  I'm using the following plugin script/config to package SparkJava into a single JAR.  It seems to work 'most' of the time.  Sometimes I need to do a clean first before the build, dunno why as yet as yet.

I thought I'd post, it might help others and if you see improvements, please share back to the group.  I'm in need of maven for dummies myself *lol*  Code below.

Cheers,

   Will

  • POM.xml
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeScope>system</excludeScope>
                            <excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
                            <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <useDefaultManifestFile>true</useDefaultManifestFile>
                    <archive>
                        <manifest>
                            <mainClass>${mainClass}</mainClass>
                            <packageName>${packageName}</packageName>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>classes/</classpathPrefix>
                        </manifest>
                        <manifestEntries>
                           <mode>development</mode>
                           <url>${pom.url}</url>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

Luiz Carlos da Silveira Júnior

unread,
Oct 1, 2014, 9:15:15 PM10/1/14
to spar...@googlegroups.com
Hi platypus,

I am newbie with maven and java, by this difficulty.
I tested the suggestion of Markus and John. Most did not work, I'm checking what I did wrong and then return the result. I will test your suggestion too.

Luiz Carlos da Silveira Júnior

unread,
Oct 2, 2014, 8:12:00 AM10/2/14
to spar...@googlegroups.com
Thank you all. 
Worked my hello! 

Basically fix all [warnings] and error "Invalid signature file digest for Manifest main attributes".

My pom.xml

Markus Mahlberg

unread,
Oct 2, 2014, 8:15:19 AM10/2/14
to spar...@googlegroups.com
Hi Luiz,

you need to replace "fully.qualified.main.Class" with something meaningful, like "com.mycompany.app.<TheClassContainingTheMainMethod>", e.g.
"com.mycompany.app.SuperWebApp".

I dont get the point why you unpack the dependencies, too. And if you really need to do it, you should definetly bind it to the "prepare-package" phase, because as of now,
the order of execution could be undetermined during packaging, iirc. If you bind it to prepare-package, you are on the safe side.

Hope this helps,

Markus

a platypus

unread,
Oct 3, 2014, 5:06:40 AM10/3/14
to spar...@googlegroups.com
G'day Luiz,

I suppose I should have included these but-s ...  of course you need to supply your own "packagename" and "MainApp" for the POM

    <properties>
        <packageName>au.com.packagename</packageName>
        <mainClass>${packageName}.MainApp</mainClass>

                      ....

        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


Anyway the best way to learn, is to DO.  I can point you to the "Maven by Example" book as as good a start as any.

Sounds like we are making progress ;-)

w.

Luiz Carlos da Silveira Júnior

unread,
Oct 3, 2014, 7:57:37 AM10/3/14
to spar...@googlegroups.com
Hi a platypus,

This book will be useful to me.
I'll make the adjustments and tests that are suggesting me.
Reply all
Reply to author
Forward
0 new messages