2

I have 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.pac</groupId>
  <artifactId>test</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>test</name>
  <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.8.2</version>
        </dependency>
    </dependencies>
    <!-- ... -->
</project>

And class and aspect:

public class Main 
{
    public int a = 10; 
    public static void main( String[] args )
    {
        Main instance = new Main();
        System.out.println(instance.test());
    }

    public int test(){
        return a;
    }
}

public aspect TestAspect {
    pointcut publicMethodExecuted(): execution(public !static * *(..));

    int around() : publicMethodExecuted() {
        System.out.println("Test string");    
        int original_return_value = proceed();
        return original_return_value * 100;
    }
}

But mvn install produce jar-file without aspectj jars. The jar looks like:

root
  |
  |--META-INF
  |
  |--com
  |   |
  |   |--pac
  |       |
  |       |--Main.class
  |
  |--TestAspect.class
  |
  |--builddef.lst

Why does the maven-jar-plugin got rid of the aspectj jars? How to fix that? I got NoClassDefFoundException at runtime:

Exception in thread "main" java.lang.NoClassDefFoundError: org/aspectj/lang/NoAspectBoundException
        at com.badmitrii.Main.test(Main.java:1)
        at com.badmitrii.Main.main(Main.java:11)
Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.NoAspectBoundException
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 2 more
user3663882
  • 6,957
  • 10
  • 51
  • 92
  • 3
    A jar does not contain dependencies only the code you have done in your package. If you wan't to have other dependencies includes you should use [maven-assembly-plugin](http://maven.apache.org/plugins/maven-assembly-plugin/) or the [maven-shade-plugin](http://maven.apache.org/plugins/maven-shade-plugin/). – khmarbaise May 15 '15 at 17:38
  • @khmarbaise Maybe you can provide an asnwer? – user3663882 May 15 '15 at 17:45
  • Use the shade plugin – user489041 May 15 '15 at 18:14

1 Answers1

2

What do you mean it got rid of the jars? were they ever in the final artifact?? Because based on your pom I don't see how that was the case. You need to use the maven-assembly-plugin to create an uberjar with all the dependencies inside. If the jars were at some point packaged then please provide more information with the changes you did when the dependencies stopped being packaged

You can see this answer for more info on how to create an uberjar, but basically, you need to add this to your pom

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>
Community
  • 1
  • 1
Hilikus
  • 9,954
  • 14
  • 65
  • 118