Guide language:
Recently, while working on a project, I introduced a package of two libraries written by the company, and the Maven repository was also deployed. And then when you compile the code and you can’t find a class, you don’t get it. At first I thought the latest package was not pulled because of local IDEA or Maven cache. Later, after a series of useless operations, it was found that the problem was that the snapshot generation of the two-party library had a timestamp, so it could not be found. Here’s how it works:
Solution steps:
1. Firstly, it was confirmed whether the version of the two-party library was consistent with that of the two-party library introduced by the project, and it was found that the version had no problem 2. Check whether the package of the binary library is really transferred to maven’s repository. Directly search the JAR package from Maven’s private server and find that the package exists, and there are many versions. Find the jar of the latest version and download it. Is the idea or Maven cache causing the same library JAR package to be used? 4. After the above three steps, the current problem is still not solved, but I think that the third party jar referenced does not have such a problem, so is it a two-party library problem? After the project is packaged, unpack the packaged JAR using a decompiler. There’s probably a couple of files in there, meta-INF, project package files. Then open meta-INF and MAINIFEST.MF, which contains the compiled configuration information. Then there is an argument called class-path, which writes the Path of the jar introduced by the compilation. Close examination reveals that the package for the introduced binary library is in the format of xxX-timestamp.jar in the class-path path. Other JAR packages have normal names. Jar format, so it is likely that the name has a timestamp. The class path contains the timestamp, but the jar in the packaged file is the name at the end of the -snapshot. Therefore, it is not possible to find the corresponding JAR directly according to the name of mainifest, so the problem is that the jar package has been introduced, but the class cannot be found when compiling.
The solution to this problem is to add a useUniqueVersions parameter to the maven package. This parameter specifies whether to use a unique version number. The default value is true. If set to false, the timestamp will be changed to the normal format of -snapshot. Mainifest class path and package name can be unified.
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.xxx.Application</mainClass> <useUniqueVersions>false</useUniqueVersions> </manifest> </archive> </configuration> </plugin>Copy the code
Here’s a link to the Maven website for a description of this parameter. A search for this question shows that this question has been raised on maven’s website for 12 years.
Conclusion:
Some things usually do not pay attention to, only understand how to use, at some time may delay a lot of time to solve. It took me a day and a half to fix this problem, which I had assumed was due to maven repository or IDAEA cache issues. This problem is not obvious, because it does see that the jar package of the binary library has been typed in, and the class file also exists. I don’t really know much about Maven, and I mainly stay in the use level. If I can solve this problem quickly, one is that I am very familiar with Maven, and the usage of each plug-in and the meaning of each parameter are very clear. The other is that I have accumulated experience. Take more notes and you’ll know what the problem is when it comes up again.
By analogy, problem solving can be divided into active solution and passive solution. Active means to make a plan when there is no problem, to have a full understanding and understanding of things, and to solve the problem immediately once it occurs. Passive solution is to understand how the problem arises and how to deal with it. It is obvious that being active is better than being passive, so when you have enough time or spare more time, you can learn, record and output actively.
References:
Maven: useUniqueVersions: no classes found in maven snapshot jar. Maven: useUniqueVersions: no classes found in Maven snapshot jar