20

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

4 Answers 4

17

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>
1
  • OK I think I understand -- when I get home I will try playing with this to see if I can make it work.
    – AlanObject
    Feb 26, 2012 at 0:11
3

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.

1
2

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.

3
  • PS: You wrote "This seems like a stupid question". It is often said that there is no such thing as a stupid question; I in fact think there are stupid questions, but yours is not one of them. +1 for asking, it helped me. Nov 29, 2016 at 5:25
  • Well it seemed like it to me at the time. These days on SO there are too many snipers who take great pride in down voting questions and I thought this might be one might be a target. But thanks for the answer.
    – AlanObject
    Nov 29, 2016 at 15:27
  • 1
    @AlanObject See new edits recommending upgrading Maven plugins versions to match latest in repo.maven.apache.org/maven2/org/apache/maven/plugins Dec 5, 2016 at 1:47
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.