0

I decided to start work on a little financial calculator for personal use and figured that it would be useful to implement an external library for handling money. I decided on JavaMoney and am still figuring out how Maven and dependencies work.

I have the following code in the POM file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>FinancialCalculator</groupId>
    <artifactId>FinancialCalculator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <dependencies>
        <dependency>
            <groupId>javax.money</groupId>
            <artifactId>money-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.javamoney</groupId>
            <artifactId>moneta</artifactId>
            <version>1.4.1</version>
            <type>pom</type>
        </dependency>
    </dependencies>
</project>

When I try and import javax.money.*;, it tells me that it cannot be resolved. I imagine I have some great misconception about how this works, so any help would be appreciated. I apologize for my ignorance.

EDIT:

I was told that I need to clean and build the project for Maven to download the appropriate JAR files. I'm using Eclipse, so I went into Project > Clean... and selected the Maven project, and I have auto-build on so it should just rebuild immediately after; but just in case, I also cleaned and manually built the project. I still can't import the javax.money package, even after cleaning and building a few times. Any help is much appreciated.

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
raidnaeem
  • 1
  • 1
  • 1
    Did you actually download the libraries? For example, did you run a "build" or a "clean and build" in your IDE (assuming you are using one)? You need to trigger that initial Maven download of the JAR files, first, before trying to use the related `import` statements. – andrewJames May 22 '22 at 22:25
  • Ah, I recall reading that I had to rebuild the index in my IDE, but I wasn't exactly sure about the process. Which repository would I rebuild? – raidnaeem May 22 '22 at 22:35
  • 1
    You only need run a regular "build" or a "clean and build" in your IDE _of your Java project_. Assuming your project is set up to use Maven (and I assume it is, given you have a pom.xml file) then _that_ is the trigger which causes Maven to see if it needs to download any dependencies (for the first time). You shouldn't need to manually trigger a rebuild of the Maven indexes, unless there are other things going on, not mentioned in your question. – andrewJames May 22 '22 at 22:42
  • Gotcha, that makes sense. I only have the one Maven project with the pom.xml file, so I shouldn't need to rebuild the indexes as you said. I did go ahead and clean and build the project, but I'm still unable to import any packages from JavaMoney. – raidnaeem May 22 '22 at 23:17
  • 1
    In that case you can edit your question: What IDE are you using? Describe the steps you took to create your Java project. Describe how you built the project. Basically, tell us how to recreate the issue you are facing. – andrewJames May 23 '22 at 00:11
  • I've checked the dependencies you've added, and everything should be correct. I got some example code to work in my IDE (see my comment on the answer below), so it's probably an issue in your Eclipse. Have you tried some of these options: https://stackoverflow.com/questions/2555845/how-to-update-maven-repository-in-eclipse – slindenau May 24 '22 at 09:00

1 Answers1

0
  1. You need to check if you are importing the correct package. GroupId and package name do not always coincide. Look into the docs of the projects to figure out what you need.

  2. Use Run As -> Maven Build ... to run the command clean verify. Look into the command line output what errors occur. These are much more reliable than those of Eclipse.

  3. If you cannot download anything, then you have probably a problem with your network or firewall.

  4. If Maven plugins can be downloaded, but your dependencies are not downloaded, you may have a typo in the name or version.

  5. You may have also a syntax error in your code, but Maven will tell you this.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • 1
    The current configuration works, it's probably an IDE reloading issue. Packages are available in central: [money api](https://mvnrepository.com/artifact/javax.money/money-api/1.1) and [implementation](https://mvnrepository.com/artifact/org.javamoney/moneta/1.4.1) . And with [sample code](https://www.programcreek.com/java-api-examples/?api=javax.money.MonetaryAmount) this works fine for me with Intellij IDEA+Maven 3. – slindenau May 23 '22 at 13:29