datafaker
datafaker copied to clipboard
Cannot use datafaker in a project that use module (module-info.java)
I am trying to use datafaker in a project that have module-info.java I am unable to add "requires net.datafaker;"
The project is maven type and the dependency is there:
To Reproduce: (I am using Eclipse)
create a project with no module-info.java create a class:
package main;
import net.datafaker.Faker;
public class Main {
public static void main(final String[] args) {
new Main();
}
public Main() {
final Faker faker = new Faker();
System.out.println(faker.name().fullName());
}
}
The code run just fine.
now go an add create module-info.java
And then the party start....
datafaker should be included in module-info.java like the rest.
module MockDataTests {
exports myUtilities;
exports dataFakerTests;
exports myHandlers;
exports javaFakerTests;
exports main;
requires io.github.classgraph;
requires io.vavr;
requires java.logging;
requires lombok;
requires org.home.myLogger;
}
and the program fail as well
Versions:
- OS: windows 10
- JDK 21
The datafaker module-info.java was only added 2 or 3 months ago. I'm not sure if it was included in release 2.1.0, you might need to use a snapshot.
https://github.com/datafaker-net/datafaker/issues/1020
The datafaker module-info.java was only added 2 or 3 months ago. I'm not sure if it was included in release 2.1.0, you might need to use a snapshot.
#1020
Something is wrong here...
Note the names... org.yaml.snakeyaml vs the content of the source
Update: I download and test datafaker-2.1.1-20240106.033820-1.jar from https://s01.oss.sonatype.org/content/repositories/snapshots/net/datafaker/datafaker/ The same pesky problem....
The module descriptor ( module-info. java ) needs to be located in the src/main/java directory.
I download datafaker-2.1.1-20240226.214407-33-sources.jar and copied the code manually into Maven project in Eclipse. I export a jar from the project that can be successfully used in module based projects. Done some testing and its look good so far.
I can upload it if you like.
Sure open a PR if you have something working. Even better (though not necessarily a blocker) if you can add tests to ensure we don't break it in the future :grinning:
I am afraid that I do not have the knowledge to do that. You have here a 71 old retired programmer (Java was not my prime environment) To visually test I use classgraph that get me all the method from scanResult.getSubclasses(BaseFaker.class) and using the result I drill down until I get to AbstractProvider level (e.g. Aviation ) and at that level I use classInfo.getDeclaredMethodInfo() and for each one I do method.invoke()
The results go to a file.
net.datafaker.providers.base.BaseFaker.txt
DataFaker: MyDataFaker.zip
The code that use classgraph Main.zip
No problem, thanks for the details. We'll try to get it sorted out.
@eitan-rosenberg Thanks for reporting this!
You mentioned this:
datafaker should be included in module-info.java like the rest.
Any reason why you can't add it to the module-info.java
yourself? I don't know exactly how these files are created, but if I do this in my project (in my module-info.java):
module demo.faker.main {
requires datafaker;
}
Then it works. I tried the same with Lombok, but I had to go through the same process by manually adding it to the module-info.java
file to make the project compile.
@erikpragt-connectid
Usually, the good people that post their libraries on Maven are doing one of two things:
- Create module-info.java in /src/main/java that ends the root folder in the jar file.
- Add "Automatic-Module-Name:" to /META-INF/MANIFEST.MF.
The file is created manually or by the IDE. Try Google for module-info.java
In the case of datafaker module-info.java was created in the correct place src/main/java/module-info.java but it ends hidden here:
and in my case, it was causing problems.
Actually I did download the source and create a working version as a proof of concept.(https://github.com/datafaker-net/datafaker/issues/1095#issuecomment-1966560278) and I upload it(https://github.com/datafaker-net/datafaker/issues/1095#issuecomment-1966650035).
You mention Lombok and as you can see that it comes already with module-info.class:
Regards.
@eitan-rosenberg can you please check whether https://github.com/datafaker-net/datafaker/pull/1108 fixes your issue or not?
@snuyanzin
@eitan-rosenberg can you please check whether #1108 fixes your issue or not?
Where is the jar for this fix ?
it could be built with using PR's branch
it seems GitHub doesn't allow to attach jar
to comments
We can merge this PR if that's easier so the snapshot can be used. If it doesn't work, no big deal, we could always revert?
We can merge this PR if that's easier so the snapshot can be used. If it doesn't work, no big deal, we could always revert?
If you can put something here: https://s01.oss.sonatype.org/content/repositories/snapshots/net/datafaker/datafaker/2.1.1-SNAPSHOT/ that would be great.
Ok, I merged the PR, the build with this whould be appeared soon among SNAPSHOTs
I just download datafaker-2.1.1-20240303.182252-38.jar and include it in a project that "requires net.datafaker" looks good so far.
<dependency>
<groupId>datafaker</groupId>
<artifactId>datafaker</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>H:\MyDownloads\datafaker-2.1.1-20240303.182252-38.jar</systemPath>
</dependency>
The compile problems are gone.
I can see module-info.class in the root of the jar using PeaZip. My test is working as well.
And a nice view.
I will use it until the next release. Thank you.
Thank you for verifying! Out of curiosity, can I ask what you are using Datafaker for and what you're building?
Thank you for verifying! Out of curiosity, can I ask what you are using Datafaker for and what you're building?
I am 72+ retired programmer and now days I code just for the fun of it (call it brain training).
I encounter DataFaker at baeldung.com, and I plan to replace JavaFaker with DataFaker.
DataFaker will be used to populate databases (I use Derby) , beans etc.
Here is a sample:
One thing that can improve the quality of the generated data is to generate data that make sense, for example, if faker.address()
would generate address object that its component are at least in the same country, I realize that this will require major restructure of the YML files, so I think that this is something for the far future (using AI maybe).
Thanks again people for your efforts.
That's an often heard challenge. I believe a library like jFairy can do it, but we haven't got a great answer to this. I believe there was something you proposed once @snuyanzin , to have data be more related? I completely agree that it would be great to have data linked somehow, but I'm not sure how we would be able to do this. For example, generating the right zipcode for the right city is already quite a challenge, and having the relationship between continent, country, city, zipcode, phone numbers, etc, I think it would be quite the project! I'm not sure if AI is the way, but some big database would be helpful.
@eitan-rosenberg if you want more brainteasers, then PRs are always welcome. It's a silly little library and a pretty nice project to just code away on.
It is a BIG challenge, but I would not take it so far as zip code and phone numbers, a more pragmatic approach can be used for example continent, country and city is very nice indeed. I am no expert, but I assume that the information is on the web and can be converted to yml http://www.geonames.org/ come to mind...
@eitan-rosenberg I don't think it's that simple: it's about a lot of connectedness between the data. For example, a person named John would be more likely to have a male gender, and a he pronoun. If the country was UK, the currency would most likely be GBP, and the airports would most likely be a British airport, the domain names should probably be a UK one, etc.
A lot of the data is possibly connected, but the addresses are just on example of this, and I believe that while absolutely valuable, goes beyond the current scope of Datafaker.
There are lots of corner cases
- Companies could have multiple addresses in multiple countries
- Some people also could have multiple addresses and in multiple countries
- Renaming of addresses keeps going
- Similar names could be in different countries
- something else
Here at https://dzone.com/articles/datafaker-a-solid-alternative-to-using-production
there is a section Exporting Data With Some Constraints
. It describes how some constraints could be added into providers and they will work during data generation. However the downside of that approach is that if we are talking about multiple relations e.g. street to city, city to country and so on... there should be multiple rules like that + at the same time there should be a relatively easy way to customize it (in case it comes with DataFaker). That's still not clear to me how to make it working