3

Following my question here I now came up to this problem. I'm using NetBeans 8.

I've created a Maven Project, let's call it MyLibMaven, (I used New Project -> Maven -> Java Aplication) and I moved my library, let's call it MyLib, into this Maven Project.

Now when I go to my normal Java Project (let's call it MyProject) and I try to add MyLibMaven as a library to MyProject I get this popup in Netbeans saying:

This project cannot be added because it does not produce a JAR file using an Ant script

What am I doing wrong here?

Here's my 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.dwnz.my.lib</groupId>
    <artifactId>MyLib</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <name>MyLib</name>

    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>17.0</version>
        </dependency>
    </dependencies>
</project>

///////////////////////// EDIT //////////////////////////////

I moved my project to a maven project and added the MyLibMaven as a dependency. But now when I try to run my project I get this failed error:

--- exec-maven-plugin:1.2.1:exec (default-cli) @ TeseMaven ---
Could not load the icon: /view/images/enable-server-icon.png
Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
    at view.toolbar.ToolBar.createIcon(ToolBar.java:171)
    at view.toolbar.ToolBar.<init>(ToolBar.java:53)
    at view.MainFrame.<init>(MainFrame.java:98)
    at view.MainFrame.newInstance(MainFrame.java:586)
    at view.Main.main(Main.java:98)
------------------------------------------------------------------------

------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 2.382s
Finished at: Wed Jul 23 02:13:22 BST 2014
Final Memory: 5M/126M
------------------------------------------------------------------------
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project MyProjectMaven: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
dazito
  • 7,740
  • 15
  • 75
  • 117
  • Adding a "Netbeans" project to an existing "Netbeans" project's libraries can only be done if the project you are trying to add is an actual "Netbeans" "project" type. Netbeans does not provide support for other project types this way. You will need to build and deploy your Maven project and then add the resulting .jar file (and it's dependencies) to your "Netbeans" project instead. A better solution would be to make your second project a Maven project as well and simply add your first project as a dependency to it...that's kind of the point of Maven – MadProgrammer Jul 23 '14 at 00:33
  • @MadProgrammer can you explain or give me a link to a tutorial on how to build and deploy my Maven project? I did try a "Clean and Build" on MyLibMaven and I see a .jar file at `...NetBeansProjects\MyLibMaven\target`. If I add that .jar file to MyProject I get the exception from my previous SO question (linked in this SO question). I just need to fix this for a quick deploy now and tomorrow I'll move MyProject to a Maven project as you suggested on the second part of your comment. – dazito Jul 23 '14 at 00:42
  • You can right click on the Maven project and select "Custom->Goals" and type deploy. This will deploy the Jar to your local repo, depending on how it's all configured – MadProgrammer Jul 23 '14 at 00:47
  • The other problem you might have, is your Maven project is not building the dependencies. I tend to use the `copy-dependencies` plugin, which copies all the dependencies to a specified location, I also included the `` plugin which allows me to add the `class-path` element to the manifest, which includes all the dependencies – MadProgrammer Jul 23 '14 at 00:53

1 Answers1

5

The main problems you are having is...

  1. Adding a Netbeans project as a library to an existing Netbeans project only works for "Netbeans Project" (ant) project types.
  2. You need to add the resulting Jar from the Maven project AND all it's dependencies to your "Netbeans" project. Because a "Netbeans (Ant) Project" has no means for resolving dependencies, you must manually resolve these dependencies and add each Jar file as a library entry.
  3. You Maven project is only producing the Jar for your project, it is not including the classes/jars from your projects dependencies.

You have at least two basic options...

You could...

Instruct Maven to include all the dependencies as part of the build process. There are (at least) two ways to achieve this. You can include all the classes into a single Jar or have Maven copy all the dependencies to a specified location and update the class-path manifest entry.

I, personally, prefer the second option, but that's a personal thing. As part of my Maven build process, I include the following...

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <!--mainClass>com.acme.MainClass</mainClass-->
                    </manifest>
                </archive>
            </configuration>
        </plugin>        

Basically, the first plugin copies all the dependency Jars to the lib directory in the ${project.build.directory}. This is a personal thing, but I don't like merging the contents of the Jar into a single "master" Jar, as I tend to have resources with the same name which I uses as lookups in my project...

The second plugin includes the class-path element within the manifest file, this ensures that the dependent jars are included in the class loader lookup path at run time...

If you want to create a "single" Jar, take a look at Including dependencies in a jar with Maven

You could...

Make your second project a Maven project and include your first project as a dependency to it. Maven will then resolve all the dependencies automatically for you. That's kind of the point...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Have you built and deployed `MyLibMaven`? – MadProgrammer Jul 23 '14 at 01:20
  • Is there any compiler errors before the "Build Failed" message – MadProgrammer Jul 23 '14 at 01:21
  • Oh my bad, I didn't paste all the message. I've edited my question. It has a null pointer exception but that file does exist and is on the correct folder – dazito Jul 23 '14 at 01:23
  • Does `/view/images/enable-server-icon.png` reside in the `src/main/resources` directory? – MadProgrammer Jul 23 '14 at 01:25
  • No, I don't have any resources folder. `enable-server-icon.png` is at `src\main\java\view\images` – dazito Jul 23 '14 at 01:29
  • Resources need to be added into `src/main/resources` directory. They will not be included from the `src/main/java` directory (I don't think anything is). Maven will package the `resources` directory with the `.class` files as part of it's build process – MadProgrammer Jul 23 '14 at 01:36
  • I'll accept your answer and +1. The problem now is with the images and I'll figure out how later on. I commented those few lines and everything is working like a piece of cake! :) Thanks a lot mate, cheers! – dazito Jul 23 '14 at 02:09
  • Just remember, Maven is fun, seriously, once you get over the configuration delight, Maven is actually a lot of fun – MadProgrammer Jul 23 '14 at 02:18