How do I create/use a class library with Netbeans

2020-05-31 09:15发布

问题:

This seems like a stupid question, but I am sort of dependent on Netbeans 7.1 wizards. There doesn't seem to be an option to do this.

My web-app is built with Maven and I want to break out some of its packages into an independently built and maintained class library (specifically, the JPA part but that shouldn't matter.) Then I want to delete those packages out of the original web application and then create a dependency to the artifact that I create.

So how do I get started? The Netbeans New-Project wizard doesn't seem have an option for "Java Class Library" like it does for ANT-built projects. The closest choice I can see is "Java Application." Is that what I use and just ignore the main class or is there some other path I don't see?

I am usually pretty good at picking this stuff up but my web searches aren't yielding much. Much thanks for any help.

回答1:

You can use the wizard for "Maven" -> "Java Application (A simple Java SE application using Maven)", as far as maven is concerned there is no difference between a libary and an application. I think netbeans will create a sample App.java that you can simply delete.

For your usecase it would make sense to also create a parent project for the library and the webapplication. Building the parent would then also build both the library and web application. It also allows you to use "build with dependencies" on the web application and have the library rebuilt first.

To create a parent project you can use the "POM Project" entry. The directory structure ideally looks like this:

- pom.xml (for parent project)
- library (folder)
    - pom.xml (for library module)
- webapp  (folder)
    - pom.xml (for webapp module)

The parent project should then contain module elements containing the relative path of your other projects:

<modules>
    <module>library</module>
    <module>webapp</module>
</modules>

The library and web application reference the parent like this, the groupId and version are defaulted to be the same as the parent:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>myGroupId</groupId>
    <artifactId>parentArtifactId</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
</parent>
<artifactId>libraryArtifactId</artifactId>


回答2:

You can begin with Maven > POM Project. Which is suitable for Class Libraries.

Also you can make your Maven project "modular". Good tutorial can be found http://sonatype.com/books/mvnex-book/reference/index.html in Chapters 8 and 9.



回答3:

The following works perfectly for class libraries in NetBeans8.1.

You will have to upgrade major plugin versions in the Maven file (and you want to remove JUnit if you are not using it, see bottom).

DO NOT use New Project -> Maven -> Java Application, it does NOT integrate with the Maven Dependency menu system in NetBeans-8.1.

Follow these steps:

  • Step1: Create a New Project -> Maven -> Project from Archtype then in the archetype search filter choose maven-archetype-quickstart.

  • Step2: Click Next and then enter your project/module name (MyLib or MyAPI) then Finish.

  • Step3: Immediately (before you forget) under Project Settings > Sources change the Source/Binary format from 1.5 to 1.8 (or other).

  • Step4: Immediately (before you forget) under Project Settings > Licence Headers choose a licence.

  • Step5: Create new (or copy from old project) Java class or interface files etc. under Source Packages in the project browser.

  • Step6: (optional) delete the public class App if you don't want to have a simple standalone module run/test class.

  • Step7: (Highly recommended): Upgrade the major Maven plugins to latest versions by editing the version numbers in the pom.xml ! The best place to find the latest versions is by inspecting the folders under the main plugins repository: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/

Under any other NetBeans Maven-based project the new "upstream" library or API project will be offered via Dependendies -> Add Dependency .. (Open Projects subtab). It will also be offered via the repository management system when you search for Maven library via Add as Dependency.

If you want to add common/shared libraries, just add them via your MyLib or MyAPI module using the Maven Dependencies feature; they will propagate to any module "downstream" that adds your MyLib or MyAPI module as a Dependency.

Optional: Also, your pom.xml will by default include JUnit:

   <dependencies>

     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>3.8.1</version>
       <scope>test</scope>
     </dependency>

Just delete it if you don't want it.



回答4:

I found a faster way of upgrading the versions of your plugins in pom.xml using NetBeans.

  1. Locate the plugins -> plugin -> version tag in your pom.xml
  2. Doubleclick the version number so that it becomes highlighted
  3. Press CTRL+SPACE to toggle autocompletion
  4. The popup menue lets you select the versions available.