1

I had to revive an old project that worked about a year and a half ago, but now when I do:

mvn clean install

either on the command line or via eclipse, it compiles fine but does not add the main-class in the manifest AND I do have the proper directive.

I'm using:

  • Apache Maven 3.5.4
  • JDK 10.0.2
  • eclipse 4.8.0 (Photon)

So here is the abbreviated version of the pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.a.b.c</groupId>
    <artifactId>JarNameHere</artifactId>
    <version>0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <log4j.version>2.4</log4j.version> 
        <http.client.version>4.5.2</http.client.version>
        <maven.compiler.source>10</maven.compiler.source>
        <maven.compiler.target>10</maven.compiler.target>
    </properties>

    <dependencies>
       <!-- DEPENDENCIES HERE, BUT REMOVED TO MAKE MORE READABLE -->
       ...
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <!-- Maven Assembly Plugin -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <!-- get all project dependencies -->
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <!-- MainClass in mainfest make a executable jar -->
                        <archive>
                            <manifest> 
                                <mainClass>com.a.b.c.MainClass</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <!-- AspectJ configuration -->
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.11</version>
                    <configuration>
                        <complianceLevel>1.11</complianceLevel>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compile</goal>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <!--  -->
            </plugins>
        </pluginManagement>
    </build>
</project>
kriegaex
  • 63,017
  • 15
  • 111
  • 202
Jerry Skidmore
  • 400
  • 2
  • 7
  • 20

2 Answers2

3

You made a classical beginner's mistake: You are assuming that the plugins configured in the pluginManagement section will automatically be used during the build. In fact, they will not. Neither AspectJ Maven nor Assembly will run like this, so of course there will not be any JAR with dependencies in your target directory.

You need to add the plugins explicitly to the plugins section, too, referencing them at least by group ID and name (no version or configuration necessary there, unless you want to override what you already defined in pluginManagement).

Besides that you will notice that AspectJ Maven is still misconfigured and the build will fail as soon as the plugin is activated. But that is outside the scope of your question, so I am not going to elaborate on that here.

P.S.: I copied your settings into my own POM, fixed them and can confirm that as soon as the AspectJ plugin has the right settings, the Assembly plugin does its job as expected including manifest with the right main class.


Update: So you only provide a POM fragment without any code to compile and run, but you want to see my full POM. I find that a bit strange because actually you should provide an MCVE. But anyway, here is what I have: I just incorporated your Assembly Plugin into one of my own projects where I usually use a One-JAR Plugin (builds an executeable JAR of JARs), replaced my plugin by yours and checked if I could run the executeable with java -jar .... The test was successful. I am still on JDK 8, though, for this question I did not upgrade my whole build system. For the example I also stripped out all my other dependencies except AspectJ runtime.

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.a.b.c</groupId>
  <artifactId>JarNameHere</artifactId>
  <version>0.1-SNAPSHOT</version>

  <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <main-class>com.a.b.c.MainClass</main-class>
    <aspectj.version>1.8.13</aspectj.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>

    <pluginManagement>
      <plugins>
        <!-- Maven Assembly Plugin -->
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-assembly-plugin</artifactId>
          <configuration>
            <!-- get all project dependencies -->
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <!-- MainClass in mainfest make a executable jar -->
            <archive>
              <manifest>
                <mainClass>${main-class}</mainClass>
              </manifest>
            </archive>
          </configuration>
          <executions>
            <execution>
              <id>make-assembly</id>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <!-- AspectJ configuration -->
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>aspectj-maven-plugin</artifactId>
          <version>1.11</version>
          <configuration>
            <!--<showWeaveInfo>true</showWeaveInfo>-->
            <source>${maven.compiler.source}</source>
            <target>${maven.compiler.target}</target>
            <Xlint>ignore</Xlint>
            <complianceLevel>${maven.compiler.target}</complianceLevel>
            <encoding>${project.build.sourceEncoding}</encoding>
            <!--<verbose>true</verbose>-->
            <!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn>-->
          </configuration>
          <executions>
            <execution>
              <phase>process-sources</phase>
              <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectj.version}</version>
        <scope>runtime</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
    </dependency>
  </dependencies>

</project>
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Could you update your answer to show how you got it to work (and include the AspectJ). This setup worked for 9 years, until the update to Maven 3 (or JDK 10) so it's either Maven updated/changed some rules or it was looser in it's compiling before and they tightened it up. Now with Maven 3, a pom.xml that worked for almost a decade stopped working – Jerry Skidmore Feb 16 '19 at 17:22
  • Done. But hey, it would be nice to not just claim that what you had before was working (because that's impossible unless you modified your POM after cut & paste) but actually show us exactly what you have. – kriegaex Feb 17 '19 at 01:59
  • I was playing around with it for a few hours and it turns out the actual cause was the upgrade to JDK 10 from Java 8. On my old laptop I had an older version of Maven 3, so it wasn't maven. This worked for me: com.github.m50d aspectj-maven-plugin 1.11.1 – Jerry Skidmore Feb 17 '19 at 07:03
  • I posted more down below – Jerry Skidmore Feb 17 '19 at 07:14
  • But your own answer shows that I was right about your missing plugin section (or you just did not copy it into your question) and I am still sure that the AspectJ configuration you showed in your question is not 100% correct, pre-Java9 or not. That the current AspectJ Maven release 1.11 does not work for Java 10 or 11 is a know fact, indeed I could have told you that currently you need to use a fork. The one I use which is also Java 11 compatible is `com.nickwongdev:aspectj-maven-plugin:1.12.1`, waiting as a PR to be included into AspectJ Maven upstream. – kriegaex Feb 17 '19 at 07:34
  • And please, next time provide a full [MCVE](http://stackoverflow.com/help/mcve) and don't forget about the error messages you get. I do not like to waste my time for writing correct answers based on the facts presented, even reproducing a described problem locally, and then the answer does not get accepted because of information hiding on the OP's side. – kriegaex Feb 17 '19 at 07:37
  • It probably isn't 100% correct, but it worked and I didn't want to study too deeply in to Maven setup (as in all I ever needed to learn about Maven could be garnered from quick Google searches vs reading up and studying the other technologies I use) – Jerry Skidmore Feb 17 '19 at 08:43
0

I have no doubt that the answer given by @kriegaex would work pre-Java 9, as my old pom.xml was working for years on Java 8. It turns out I didn't give enough info in my question to properly answer this. It was my upgrade from Java 8 to Java 10 that messed up the AspectJ Integration, so it would fail before Maven got to the creation of the jar.

Note: I will continue to refine this as it may be better to use the pluginManagement tag. Unfortunately, I don't modify the pom.xml that often other than dependency tags which are easy. So I get good for a short period working through hard things only to never touch/make major changes for years forgetting all I learned the previous time through.

I found the solution here: Maven AspectJ plugin fails to build with Java 9 due to missing tools.jar

THE ACTUAL SOLUTION: Changing a few lines in my pom.xml did the trick:

            <groupId>com.github.m50d</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.11.1</version>
<!--
            THESE WERE THE ORIGINAL LINES
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.11</version>
 -->                

I figured this out doing this on the command line:

mvn -X clean install

Even the latest version of AspectJ was still looking for the tools.jar file:

<properties>
    <!-- Other lines removed for brevity -->
    <aspect.version>1.9.1</aspect.version>
</properties>

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.machinepublishers/jbrowserdriver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspect.version}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>${aspect.version}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${aspect.version}</version>
    </dependency>

    <!-- OTHER DEPENDENCIES REMOVED FOR BREVITY -->
 </dependencies>
<build>
    <plugins>
        <!-- Maven Assembly Plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <!-- get all project dependencies -->
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <!-- MainClass in mainfest make a executable jar -->
                        <archive>
                            <manifest>   
  <mainClass>a.b.c.Foo</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- AspectJ configuration -->
        <plugin>
            <groupId>com.github.m50d</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.11.1</version>
<!--
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.11</version>
 -->                
            <configuration>
                <!-- MUST use 10, NOT 1.10 or ajc breaks -->
                <complianceLevel>10</complianceLevel>
                <source>10</source>
                <target>10</target>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <Xlint>ignore</Xlint>
                <encoding>UTF-8</encoding>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
                        <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
                    </goals>
                </execution>
            </executions>
        </plugin>
     <!--  -->
    </plugins>
</build>
Jerry Skidmore
  • 400
  • 2
  • 7
  • 20