Can we use PrettyTime in a controller?

Hi all,

Has anyone ever had any luck in using PrettyTime in a controller? I can’t get this simple Maven dependency to work:



prettytime-4.0.0.Final-sources.jar appears under Maven Dependencies (in Eclipse). Everything is fine at compile time, but then at run-time I get a java.lang.ClassNotFoundException: org.ocpsoft.prettytime.PrettyTime that goes away as soon as I comment the PrettyTime invocation:

modelLastUpdated = (new PrettyTime(Context.getLocale())).format(earliestObsDate);

Any help (or alternatives) would be appreciated. Thanks!

You need to remove <scope>provided</scope>, because that tells maven to exclude it from the generated .omod file assuming it will somehow be present at run time, you only set scope provided to a dependency if you are sure something else provides it e.g the servlet container or another module or Openmrs platform

Hi @wyclif, thanks for your response.

Sorry, I left the <scope>provided</scope> directive when I pasted my POM’s snippet as it was one of my latest trials. At first I tried without it (as I would do for any new Maven dependency). It didn’t work so I started to poke around. I thought that maybe, somehow, the JAR (or some JAR that it would depend on) was provided twice and that this could be the cause of the ClassNotFoundException… but no.

Can you open the generated .omod file and make sure that the dependency jar file is included inside the lib folder? If it exists, most likely there is something else providing it too and you end up with duplicate jars at run time, you might consider running maven dependency:tree goal to confirm that it is not a duplicate jar

It is not a duplicate JAR and it is indeed in the packaged lib subfolder. To provide some additional hint(s): this problem does not happen in my test environment. At first I tried PrettyTime in a JUnit test routine and it worked perfectly well.

Also I have a dependency on Joda-Time that works and that is made exactly the same way in omod/pom.xml. Hence they show up the same way as well out of mvn dependency:tree:

[INFO] +- joda-time:joda-time:jar:RELEASE:compile
[INFO] +- org.ocpsoft.prettytime:prettytime:jar:RELEASE:compile

I mean inspect the entire dependency:tree output to ensure it is not provided by another dependency already, can you share your git repo?

Yes, I did check that there were no double in the output of mvn dependency:tree, the line I pasted previously (+- org.ocpsoft.prettytime:prettytime:jar:RELEASE:compile) is the only occurrence of PrettyTime in there.

Sure it would be great if you could take a closer look! Here is the branch you can clone to reproduce the issue. And also you would need to uncomment this line in order to provoke the PrettyTime call at run-time.

The root pom file of the branch you pointed me to actually doesn’t have that dependency

On the branch that you see it is the omod/pom.xml that contains the dependency. I had tried also to bring this dependency to the root pom.xml before to no avail. But for Joda-Time, I just have it in omod/pom.xml and it works fine (for a dependency whose classes are only used in a controller, so to say ‘on the omod side’). Just to check I tried to add it both in omod/pom.xml and in the root pom.xml, same problem.

If you added the dependency and it is getting packaged with omod but the class can’t be found at runtime, am not sure what is going wrong, my only guess is that there is a duplicate at runtime coming from somewhere and it might be tough to figure out where it is coming from. You might want to stop your module and try loading the class somewhere in your code with the call to Context.loadClass and see if it already exists. If it does you need to change its scope to provided.

I have just tried running your module with that line uncommented and scope provided removed from the pom file. All works well.

That is what i suspected, it is a duplicate dependency coming from somewhere else

… meaning some other module? This would mean that @dkayiwa doesn’t run my module with the same set of other modules.

In which case <scope>provided</scope> should have worked for me, no?

I did run your module with all the reference application modules. So Wyclif says that you may be having other custom modules that duplicate this.

I just diff’d the referenceapplication-package-2.2 package and our dev modules directory and here are the two differences:

  1. addresshierarchy-2.8-SNAPSHOT.omod
  2. referenceapplication-2.3-SNAPSHOT.omod

So I got the Maven dependency tree for those two and none of them references PrettyTime. @dkayiwa, how did you opbtain your ref app modules?

Also then, if this JAR was provided through some other module, <scope>provided</scope> would have worked right?

Here are the modules am using:

Try running the dependency tree at as below:

mvn dependency:tree -Dverbose -Dincludes=groupId:artifactId

This won’t help though if you have any modules that exist at runtime and not defined as dependencies in your project