datafaker icon indicating copy to clipboard operation
datafaker copied to clipboard

Cannot use datafaker in a project that use module (module-info.java)

Open eitan-rosenberg opened this issue 1 year ago • 6 comments

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: image

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 image

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

image

Versions:

  • OS: windows 10
  • JDK 21

eitan-rosenberg avatar Feb 24 '24 18:02 eitan-rosenberg

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

kingthorin avatar Feb 24 '24 18:02 kingthorin

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

image

eitan-rosenberg avatar Feb 24 '24 19:02 eitan-rosenberg

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....

eitan-rosenberg avatar Feb 25 '24 11:02 eitan-rosenberg

The module descriptor ( module-info. java ) needs to be located in the src/main/java directory.

eitan-rosenberg avatar Feb 25 '24 12:02 eitan-rosenberg

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.

image

I can upload it if you like.

eitan-rosenberg avatar Feb 27 '24 13:02 eitan-rosenberg

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:

kingthorin avatar Feb 27 '24 13:02 kingthorin

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

eitan-rosenberg avatar Feb 27 '24 14:02 eitan-rosenberg

No problem, thanks for the details. We'll try to get it sorted out.

kingthorin avatar Feb 27 '24 14:02 kingthorin

@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 avatar Mar 02 '24 14:03 erikpragt-connectid

@erikpragt-connectid

Usually, the good people that post their libraries on Maven are doing one of two things:

  1. Create module-info.java in /src/main/java that ends the root folder in the jar file.
  2. 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: image 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: image

Regards.

eitan-rosenberg avatar Mar 02 '24 15:03 eitan-rosenberg

@eitan-rosenberg can you please check whether https://github.com/datafaker-net/datafaker/pull/1108 fixes your issue or not?

snuyanzin avatar Mar 02 '24 23:03 snuyanzin

@snuyanzin

@eitan-rosenberg can you please check whether #1108 fixes your issue or not?

Where is the jar for this fix ?

eitan-rosenberg avatar Mar 03 '24 06:03 eitan-rosenberg

it could be built with using PR's branch it seems GitHub doesn't allow to attach jar to comments

snuyanzin avatar Mar 03 '24 09:03 snuyanzin

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?

bodiam avatar Mar 03 '24 13:03 bodiam

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.

eitan-rosenberg avatar Mar 03 '24 13:03 eitan-rosenberg

Ok, I merged the PR, the build with this whould be appeared soon among SNAPSHOTs

snuyanzin avatar Mar 03 '24 18:03 snuyanzin

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. image

I will use it until the next release. Thank you.

eitan-rosenberg avatar Mar 03 '24 20:03 eitan-rosenberg

Thank you for verifying! Out of curiosity, can I ask what you are using Datafaker for and what you're building?

bodiam avatar Mar 03 '24 21:03 bodiam

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: image

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). image

Thanks again people for your efforts.

eitan-rosenberg avatar Mar 04 '24 07:03 eitan-rosenberg

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.

bodiam avatar Mar 04 '24 10:03 bodiam

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 avatar Mar 04 '24 11:03 eitan-rosenberg

@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.

bodiam avatar Mar 04 '24 12:03 bodiam

There are lots of corner cases

  1. Companies could have multiple addresses in multiple countries
  2. Some people also could have multiple addresses and in multiple countries
  3. Renaming of addresses keeps going
  4. Similar names could be in different countries
  5. 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

snuyanzin avatar Mar 04 '24 12:03 snuyanzin