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

后端 未结 1 1946
时光说笑
时光说笑 2020-12-06 22:54

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 -> Mav

相关标签:
1条回答
  • 2020-12-06 23:14

    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...

    0 讨论(0)
提交回复
热议问题