12

My POM contained:

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-configuration2</artifactId>
        <version>2.3</version>
    </dependency>

Both sample codes from Quick start guide, Reading a properties file:

Configurations configs = new Configurations();
try
{
    Configuration config = configs.properties(new File("config.properties"));
    // access configuration properties
    ...
}
catch (ConfigurationException cex)
{
    // Something went wrong
}

and Properties files, Using PropertiesConfiguration:

Parameters params = new Parameters();
FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
    new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
    .configure(params.properties()
        .setFileName("usergui.properties"));
try
{
    Configuration config = builder.getConfiguration();
    ...
}
catch(ConfigurationException cex)
{
    // loading of the configuration file failed
}

throwed:

java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean
...
at org.apache.commons.configuration2.builder.fluent.Parameters.createParametersProxy(Parameters.java:307)
at org.apache.commons.configuration2.builder.fluent.Parameters.fileBased(Parameters.java:186)
at properties.PropertiesTest.testLoadAndStoreWithCommonsConfiguration(PropertiesTest.java:52)
...
Caused by: java.lang.ClassNotFoundException: org.apache.commons.beanutils.DynaBean

mvn dependency:tree showed:

...
[INFO] +- org.apache.commons:commons-configuration2:jar:2.2:compile
[INFO] |  +- org.apache.commons:commons-lang3:jar:3.6:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.2:compile
...

commons-configuration2's POM contains:

    ...
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.3</version>
        <optional>true</optional><
    /dependency>
    ...
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • The dependency is in the POM since at least 2011, but declared as "optional". I suppose it's the reason why gradle does not include it per default. The reason for optional is not clear: https://github.com/apache/commons-configuration/commit/3074a153514c28f853e181f0667748fb22037119 – Olivier Faucheux Jan 13 '22 at 08:18

2 Answers2

19

I added the following dependency to my POM and it worked:

    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.3</version>
    </dependency>

UPDATE

The latest POM of commons-configuration2 (as of Oct '21) declares <version>1.9.4. I didn't try that but it may work with later versions of Commons Configuration.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
-2
<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.4</version>
</dependency>

After adding the code in pom.xml, I found that it doesn't work.

I downloaded the jar, put it into lib content under Web-INF, then it works.

I think that Maven's plugin log causes it to die, it can not update or delete the jar automatically.

David Buck
  • 3,752
  • 35
  • 31
  • 35
Miozus
  • 1
  • 1
  • 2
    Putting dependencies into `lib` folders is old-school and definitely not the canonical Maven way. Which Maven plugin are you referring to? – Gerold Broser Oct 29 '21 at 22:36