Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven dependencies not being compiled

I have been trying for the last hour or so to get my Maven project to include source files from its dependencies, but for some reason, it isn't. I have followed the steps provided by the following link, but when I compile and run the plugin, I get a ClassNotFoundException:

https://github.com/mkremins/fanciful

I have made sure to include the dependencies and the repository from the link above into my pom.xml file, but when I compile, they don't get added to my .jar file.

I am fairly new to using Maven, and like it so far, albeit that it can be a pain to solve issues like this.

I am building the project by doing the following:

Right click project -> Run As -> Maven Build -> Goal: clean install

  • EDIT -

With a little more searching around, I figured it wasn't as easy as I thought so. I added the following to my pom.xml build section:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.1</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <createDependencyReducedPom>false</createDependencyReducedPom>
          <minimizeJar>true</minimizeJar>
          <artifactSet>
              <includes>
                  <include>mkremins:fanciful</include>
                  <include>org.json:json</include>
              </includes>
          </artifactSet>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

The only problem with this is that I needed to also manually include the dependencies of the main library I wanted to use - mkremins:fanciful; is there flag or option to automatically copy dependencies from the one file I need, rather than also including <include>org.json:json</include>?

like image 483
Dragonphase Avatar asked Nov 02 '22 04:11

Dragonphase


1 Answers

Well, if you want to have your dependencies copied to your target jar, you need to tell maven to do so! Maven doesn't know if the artifact of your project is meant to be self-sufficient executable jar, jar to be executed inside a container or just a dependency or library for another project.

You might want to use copy-dependencies task from maven-dependency-plugin

For example:

    <build>

            <plugins>

                <plugin>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy-dependencies</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <includeScope>runtime</includeScope>
                                <outputDirectory>${project.build.directory}</outputDirectory>

                                <excludeTransitive>false</excludeTransitive>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
</plugins>
</build>

For more tweaking you might also want to play with jar plugin and assembly plugin. On more about creating executable jars:

http://www.ibm.com/developerworks/java/library/j-5things13/index.html?ca=dat-

like image 98
Kranach Avatar answered Nov 08 '22 06:11

Kranach