Home > Software engineering >  IntelliJ: How to Import a Package I Created
IntelliJ: How to Import a Package I Created

Time:09-17

I am working on various Java projects using intelliJ as my IDE. I am also using Maven to build the project. All my projects link to a single database. As such, I have been developing a set of helper functions that are useful in various projects as they come up. I want to put those helper functions in a package (MyPackage), and then just import them when I need them, just like I would import java.util.ArrayList; for example.

Based on my review of intelliJ site, I looked specifically at the use of libraries. https://www.jetbrains.com/help/idea/library.html. I also looked at a youtube video.

The first step is to "define" your library. The instructions are unclear was to what project you need to be in for this step: (i) the project you created your library in; or (ii) the project you want to import the library into. I assume it is (i)? The text is so small on the youtube videos its impossible to see.

Then, you add the library to your module, which is the project that you want to add the library to. Again you go to Project Structure, and add just the .jar file. I think (think) I have done that, since in that .jar file shows up in the project I am working on.

I am using MAVEN to import Maven libraries, but it is unclear to me whether I need to add myPackage as a dependency to the .pom file or not, since myPackage is not a Maven file.

When my .jar file appears in my library, it only has it's ArtifactId - Version whereas, I notice other libraries have GroupId:ArtifactId-version.

If anyone has any suggestions, it would be appreciated. Been trying everything for 2 days here.

CodePudding user response:

Maven's biggest advantage is helping you manage your dependencies without adding .jar files directly in your project. First thing that comes to my mind is this:

  1. Create normal Maven project in IntelliJ IDEA: File >> New >> Project >> Select Maven >> Next >> Under "Artifact Coordinates" option write what you want for your projects ArtifacId, GroupId and Version to be >> Finish creating project.
  2. When done creating it, check pom.xml file of newly created project, it should have ArtifactId, GroupId and Version you just wrote.
  3. Create packages and write business logic.
  4. Run maven clean install goals (mvn clean install in cmd line or do this in "Edit Configurations" tab in IntelliJ) on the project. This will create .jar file in the path "C:\Users<UserName>\.m2\repository<groupId of project>" (by default on Windows).
  5. You can create dependency of your project as any other library in your other projects pom.xml file with artifactId groupId and version that you have in newly created project.

For instance if your new projects properties are:

<groupId>org.example</groupId>
<artifactId>Test</artifactId>
<version>1.0-SNAPSHOT</version>

then your dependency should look like this:

<dependency>
        <groupId>org.example</groupId>
        <artifactId>Test</artifactId>
        <version>1.0-SNAPSHOT</version>
</dependency>
  • Related