How do I create/use a class library with Netbeans on top of Maven?

久未见 提交于 2019-12-03 04:54:44
Jörn Horstmann

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>

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!