0

I'm writting the sameple AspectJ project - github. I've created the following 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.badmitrii</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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <manifestEntries>
                            <Class-Path>config/</Class-Path>
                        </manifestEntries>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <classpathLayoutType>custom</classpathLayoutType>
                            <customClasspathLayout>$${artifact.groupId}.$${artifact.artifactId}.$${artifact.extension}</customClasspathLayout>
                            <mainClass>com.badmitrii.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
               <artifactId>maven-assembly-plugin</artifactId>
               <configuration>
                 <archive>
                   <manifest>


      <mainClass>com.badmitrii.Main</mainClass>
               </manifest>
             </archive>
             <descriptorRefs>
               <descriptorRef>assembly</descriptorRef>
             </descriptorRefs>
           </configuration>
        </plugin>
    </plugins>
</build>

and assembly.xml:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

    <id>assembly</id>

    <formats>
        <format>zip</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <outputFileNameMapping>${artifact.groupId}.${artifact.artifactId}.${artifact.extension}</outputFileNameMapping>
            <useProjectArtifact>false</useProjectArtifact>
            <!-- you may place excludes here -->
        </dependencySet>
    </dependencySets>

    <files>
        <file>
            <outputDirectory>/</outputDirectory>
            <source>${project.build.directory}/${project.artifactId}-${project.version}.jar</source>
            <destName>${project.artifactId}.jar</destName>
        </file>
    </files>

    <fileSets>
        <fileSet>
            <outputDirectory>config</outputDirectory>
            <directory>config</directory>
        </fileSet>
        <fileSet>
            <outputDirectory>/</outputDirectory>
            <directory>src/main/bin</directory>
        </fileSet>
    </fileSets>

</assembly>

But my jar produced by mvn install still doesn't contain the dependencies declared in the <dependencies> tag in pom.xml. Actually, I have the following jar:

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

And when I try to execute that jar I get

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:9)
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

How can I include the dependencies into the jar to avoid throwing the exception?

As you can see from the pom.xml file I have already included maven-assembly-plugin declaration, but it doesn't include the dependencies into the jar.

I do the following to compile and run the project:

mvn install
java -jar ./target/test-1.0-SNAPSHOT.jar

It seems, the plugin doesn't even run. Actually mvn install | grep 'maven' prints the following:

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test ---
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test ---
[INFO] --- aspectj-maven-plugin:1.7:compile (compile) @ test ---
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test ---
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test ---
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ test ---
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ test ---
[INFO] --- maven-install-plugin:2.4:install (default-install) @ test ---
user3663882
  • 6,957
  • 10
  • 51
  • 92
  • @djeikyb Have you ever read my question? If so, you would have noticed that I included `maven-assembly-plugin` in `pom.xml`. My question is how to make that plugin work. – user3663882 May 16 '15 at 06:05
  • Can You share exactly how do You run? The whole command. – Rekin May 16 '15 at 06:12
  • I did! I made a similar mistake when i was learning about this too. I highly recommend the shade plugin. Both it and the old assembly plugin way are addressed by answers there. – djeikyb May 16 '15 at 06:14
  • Thanks. One thing that interested me is, did the assembly plugin actually run? I see it's declaration in build/plugins, but I don't see any "phase" binding... – Rekin May 16 '15 at 06:16
  • @Rekin I re-ran it just now and I didn't notice something like `--- maven-assembly-plugin` in the `mvn install`'s output. Does it mean that it didn't run? – user3663882 May 16 '15 at 06:21
  • @Rekin I've added the output of `mvn install | grep 'maven'` – user3663882 May 16 '15 at 06:23
  • @user3663882: Thanks, I've checked it. The "execution" is missing. Therefore, the assembly plugin isn't even run. But, after I bound it locally, I get another error: "descriptor with ID 'assembly' not found" – Rekin May 16 '15 at 06:26

1 Answers1

1
  1. You're missing execution from assembly configuration.
  2. The descriptorRef tag refers to 'prefabricated' default setups. You should use the "desriptor" tag instead.
  3. The assembly being generated has ".zip" extension and includes other jar files, which java won't unpack by itself.

We could go on and systematically correct all the remaining mistakes, but we would end up exactly with what the maven-shade-plugin does out of the box. You really should reconsider using it.

Rekin
  • 9,731
  • 2
  • 24
  • 38