Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is it possible that Maven builds classes that are not in my project?

Tags:

java

maven

I'm seeing an extremely strange issue with Maven today: it's trying to build classes that are not in my project, but coming from dependencies. mvn compile crashes with:

...
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] AppConstants.java):[4,12] unmappable character for encoding UTF-8
...

There is no AppConstants.java in my project, and a search with grep -r AppConstants . gives no results. The class exists in another artifact that's a dependency of my project.

I managed to get my project to compile by passing -Dproject.build.sourceEncoding=Cp1252 to mvn compile, but this is not a solution, because my project should use utf-8 encoding. Sure enough, when compiled this way I see AppConstants.class under target/classes/ but where can it come from? How is this possible?

Nothing changed in the pom of the project recently, but I know that the dependent project is working on migrating to Maven these days. What could be going on?

UPDATE

There is no AppConstants.java file in my project, and AppConstants is not mentioned anywhere within the project:

$ grep -ri AppConstants .         # finds nothing
$ find . | grep -i AppConstants   # finds nothing

This AppConstants exists in another project that mine depends on. I don't have the source code of that other project on my PC. My teammates can all reproduce this problem, it's not specific to my PC. Our pom hasn't changed recently, but we know the pom of the other team is changing a lot these days.

There is one class in my project that uses SomeOther class from the other project. If I build using -Dproject.build.sourceEncoding=Cp1252, then the build works, and in my target folder I see SomeOther.class, AppConstants.class, and a few other clases from the other project generated. If I remove from my project the class that uses SomeOther, then I can build normally, though the project will not work, as it needs those other classes.

@Gus: Knowing that it's in SomeOther, can you make a public-safe sample project that exhibits the same behavior? Failing that, describe the heirarchy, and where you're running mvn compile from... do you have a multi-module project, and you're building a subproject?

Too hard to create a public-safe sample. My project is multi-module, when I run mvn compile at the top, it fails at a submodule, so for faster iteration I've been running in the submodule instead. The result is the same but faster. But you made me think: I will check if our builds generate .class files for other projects in other submodules.

Sharing code and the full pom

This is at work, so I cannot share complete code and pom, unfortunately. I can include anonymized snippets, if you ask for specific sections.

like image 913
janos Avatar asked Jan 13 '14 11:01

janos


People also ask

How Maven project is different than any other normal Java project?

Maven is a powerful project management tool that is based on POM (project object model). It is used for projects build, dependency and documentation. How Maven solves the following problems we face in project management: It adds all the necessary jars to the project as per the dependencies put ny user in pom.

How does Maven build a project?

Maven will start building the project. We give maven two goals, first to clean the target directory (clean) and then package the project build output as jar (package). Packaged jar is available in consumerBanking\target folder as consumerBanking-1.0-SNAPSHOT.

What is the use of Maven and explain how is it used to take build?

Maven is written in Java and is used to build projects written in C#, Scala, Ruby, etc. Based on the Project Object Model (POM), this tool has made the lives of Java developers easier while developing reports, checks build and testing automation setups.

What is a groupId and artifactId in Maven?

groupId – a unique base name of the company or group that created the project. artifactId – a unique name of the project. version – a version of the project.


1 Answers

Figured it out: the other team mistakenly put .java files into their jar artifact, instead of .class files.

So, apparently, when Maven doesn't find the compiled classes but it finds the source code, then it builds the classes locally from that source code. Which is pretty clever, and now everything makes perfect sense.

After the other team fixed their jars, and we refreshed our Maven artifact caches, the build works again, using our utf-8 encoding and no hackish workarounds, we're back to sanity.

@andyf, @khmarbaise, @PavelHoral, @Gus, thank you guys for your help!

like image 161
janos Avatar answered Oct 26 '22 04:10

janos