问题描述
在ProjectA中,我在MethodA
中有一个ClassA
,在不同的项目中添加了ProjectA jar作为Maven依赖,不同的项目在调用{{1} }.
要求是
每当 MethodA
的 MethodA
被任何其他项目调用时,我们都需要记录被调用的项目工件 ID 和版本,考虑到这些项目 pom.xml 中添加了 ProjectA 依赖项。
注意
以下仅适用于自项目(ProjectA),创建属性文件并开启maven 资源过滤
ClassA
方法A
Create a property file
src/main/resources/project.properties
with the below content
version=${project.version}
artifactId=${project.artifactId}
Now turn on maven resource filtering
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
在项目 B 中调用 MethodA 时,我在记录器中得到以下输出
public class ClassA {
final static Logger logger = Logger.getLogger(ClassA.class);
public void MethodA{
final Properties properties = new Properties();
properties.load(this.getClassLoader().getResourceAsstream("project.properties"));
logger.info(properties.getProperty("version"));
logger.info(properties.getProperty("artifactId"));
}
}
预期输出:
version=${project.version}
artifactId=${project.artifactId} which is incorrect.
有没有更好的方法来记录调用项目的工件 ID?如果 version = 1.0.0
artifactId = ProjectB
被 ProjectC 调用,我们想要获取 ProjectC artifactid 和 version。
要求:我们有 30 多个项目从 ProjectA 中调用 MethodA,因此我们不应对调用项目进行任何更改。
解决方法
方案A:Maven资源过滤
如果你把它放在正确的 POM 部分,你的 POM 片段应该正确替换变量:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
您可以在 target/classes
文件夹中检查结果。在我通过向您的方法名称添加一个空参数列表 ()
并将无意义的 this.getClassLoader()
替换为 getClass().getClassLoader()
来修复您的错误伪代码之后,该代码甚至可以编译并执行一些有意义的操作。在向 StackOverflow 等公共平台发布内容之前,您是否进行过测试?
import java.io.IOException;
import java.util.Properties;
public class ClassA {
public void methodA() throws IOException {
final Properties properties = new Properties();
properties.load(getClass().getClassLoader().getResourceAsStream("project.properties"));
System.out.println(properties.getProperty("version"));
System.out.println(properties.getProperty("artifactId"));
}
public static void main(String[] args) throws IOException {
new ClassA().methodA();
}
}
在 mvn compile
之后从 IntelliJ IDEA 运行时的控制台日志(因为我们需要 Maven 来处理资源并将它们复制到 target/classes
):
1.9.8-SNAPSHOT
util
或者不管你的模块名称和版本是什么。
如果您看到的是变量名,要么您的类路径不指向 JAR,而是指向源目录,要么您有多个带有 project.properties
文件的模块,并且其中一个忘记了资源过滤。无论哪个文件首先在类路径上找到,都将被加载。所以在一个多模块的项目中,最好使用不同的文件名,否则哪个第一个找到就或多或少是个抽奖。
接下来的问题是您的方面或其他模块知道要加载哪个资源文件,以便更好地以某种方式链接到类或包名称,以便其他模块能够从包裹名字。那么您确实需要在模块之间进行干净的包名分离。我真的想知道这是否值得麻烦。
方案 B:模板化 Maven 插件 + package-info.java
+ 自定义注解
另一个想法是使用资源过滤或像 org.codehaus.mojo:templating-maven-plugin
这样的插件将版本直接替换为 package annotation values in a package-info.java
file,然后在运行时简单地从包信息中获取值。我使用该插件进行了快速而肮脏的本地测试,效果很好。我建议现在保持简单,只解决你的资源过滤问题。如果您需要我刚刚描述的更通用的解决方案,请告诉我。
项目结构
更新:我将侵入我的一个项目的快速解决方案提取到一个新的 Maven 多模块项目中,以便向您展示一个干净的解决方案,如下所示:
假设我们有一个包含 3 个子模块的父 POM:
-
annotation
- 包含要在package-info.java
文件中的包上使用的注释。可以轻松修改为也适用于类。 -
library
- 应用程序模块要访问的示例库 -
application
- 示例应用
您可以在 GitHub 上找到完整的项目: https://github.com/kriegaex/SO_Maven_ArtifactInfoRuntime_68321439
项目的目录布局如下:
$ tree
.
├── annotation
│ ├── pom.xml
│ └── src
│ └── main
│ └── java
│ └── de
│ └── scrum_master
│ └── stackoverflow
│ └── q68321439
│ └── annotation
│ └── MavenModuleInfo.java
├── application
│ ├── pom.xml
│ └── src
│ ├── main
│ │ ├── java
│ │ │ └── de
│ │ │ └── scrum_master
│ │ │ └── stackoverflow
│ │ │ └── q68321439
│ │ │ └── application
│ │ │ └── Application.java
│ │ └── java-templates
│ │ └── de
│ │ └── scrum_master
│ │ └── stackoverflow
│ │ └── q68321439
│ │ └── application
│ │ └── package-info.java
│ └── test
│ └── java
│ └── de
│ └── scrum_master
│ └── stackoverflow
│ └── q68321439
│ └── application
│ └── ModuleInfoTest.java
├── library
│ ├── pom.xml
│ └── src
│ └── main
│ ├── java
│ │ └── de
│ │ └── scrum_master
│ │ └── stackoverflow
│ │ └── q68321439
│ │ └── library
│ │ └── LibraryClass.java
│ └── java-templates
│ └── de
│ └── scrum_master
│ └── stackoverflow
│ └── q68321439
│ └── library
│ └── package-info.java
└── pom.xml
请注意库和应用程序模块中的 src/java-templates
目录,其中包含 package-info.java
文件。目录名称是 Templating Maven Plugin 的默认名称,使插件配置不那么冗长。
父 POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.scrum-master.stackoverflow.q68321439</groupId>
<artifactId>maven-artifact-info-runtime</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<modules>
<module>annotation</module>
<module>library</module>
<module>application</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>filter-src</id>
<goals>
<goal>filter-sources</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
模块annotation
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.scrum-master.stackoverflow.q68321439</groupId>
<artifactId>maven-artifact-info-runtime</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>annotation</artifactId>
</project>
package de.scrum_master.stackoverflow.q68321439.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PACKAGE)
public @interface MavenModuleInfo {
String groupId();
String artifactId();
String version();
}
模块library
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.scrum-master.stackoverflow.q68321439</groupId>
<artifactId>maven-artifact-info-runtime</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>library</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>de.scrum-master.stackoverflow.q68321439</groupId>
<artifactId>annotation</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
package de.scrum_master.stackoverflow.q68321439.library;
public class LibraryClass {}
请注意,以下文件需要位于 library/src/main/java-templates/de/scrum_master/stackoverflow/q68321439/library/package-info.java
中。在这里你可以看到我们如何使用 Maven 属性在构建过程中通过 Maven 模板插件替换为其对应的值:
/**
* This is the package description (...)
*/
@MavenModuleInfo(groupId = "${project.groupId}",artifactId = "${project.artifactId}",version = "${project.version}")
package de.scrum_master.stackoverflow.q68321439.library;
import de.scrum_master.stackoverflow.q68321439.annotation.MavenModuleInfo;
模块application
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.scrum-master.stackoverflow.q68321439</groupId>
<artifactId>maven-artifact-info-runtime</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>application</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>de.scrum-master.stackoverflow.q68321439</groupId>
<artifactId>annotation</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>de.scrum-master.stackoverflow.q68321439</groupId>
<artifactId>library</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>de.scrum-master.stackoverflow.q68321439</groupId>
<artifactId>library</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package de.scrum_master.stackoverflow.q68321439.application;
public class Application {
public static void main(String[] args) {}
}
请注意,以下文件需要位于 application/src/main/java-templates/de/scrum_master/stackoverflow/q68321439/application/package-info.java
中。在这里你可以看到我们如何使用 Maven 属性在构建过程中通过 Maven 模板插件替换为其对应的值:
/**
* This is the package description (...)
*/
@MavenModuleInfo(groupId = "${project.groupId}",version = "${project.version}")
package de.scrum_master.stackoverflow.q68321439.application;
import de.scrum_master.stackoverflow.q68321439.annotation.MavenModuleInfo;
package de.scrum_master.stackoverflow.q68321439.application;
import de.scrum_master.stackoverflow.q68321439.annotation.MavenModuleInfo;
import de.scrum_master.stackoverflow.q68321439.library.LibraryClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ModuleInfoTest {
@Test
public void test() {
String groupId = "de.scrum-master.stackoverflow.q68321439";
MavenModuleInfo libMavenInfo = logAndGetMavenModuleInfo("Library Maven info",LibraryClass.class.getPackage());
assertEquals(groupId,libMavenInfo.groupId());
assertEquals("library",libMavenInfo.artifactId());
MavenModuleInfo appMavenInfo = logAndGetMavenModuleInfo("Application Maven info",Application.class.getPackage());
assertEquals(groupId,appMavenInfo.groupId());
assertEquals("application",appMavenInfo.artifactId());
}
private MavenModuleInfo logAndGetMavenModuleInfo(String message,Package aPackage) {
MavenModuleInfo moduleInfo = aPackage.getAnnotation(MavenModuleInfo.class);
System.out.println(message);
System.out.println(" " + moduleInfo.groupId());
System.out.println(" " + moduleInfo.artifactId());
System.out.println(" " + moduleInfo.version());
return moduleInfo;
}
}
运行 Maven 构建
现在通过 mvn clean test
运行 Maven 构建:
(...)
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ application ---
[INFO] Surefire report directory: C:\Users\alexa\Documents\java-src\SO_Maven_ArtifactInfoRuntime_68321439\application\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running de.scrum_master.stackoverflow.q68321439.application.ModuleInfoTest
Library Maven info
de.scrum-master.stackoverflow.q68321439
library
1.0-SNAPSHOT
Application Maven info
de.scrum-master.stackoverflow.q68321439
application
1.0-SNAPSHOT
Tests run: 1,Failures: 0,Errors: 0,Skipped: 0,Time elapsed: 0.094 sec
(...)
识别来电者
假设所有调用模块都实现了相同的包信息+特殊注释的方案,你可以像这样打印调用者信息:
package de.scrum_master.stackoverflow.q68321439.library;
import de.scrum_master.stackoverflow.q68321439.annotation.MavenModuleInfo;
public class LibraryClass {
public void doSomething() {
StackTraceElement callerStackTraceElement = new Exception().getStackTrace()[1];
try {
Class<?> callerClass = Class.forName(callerStackTraceElement.getClassName());
MavenModuleInfo mavenModuleInfo = callerClass.getPackage().getAnnotation(MavenModuleInfo.class);
System.out.println(mavenModuleInfo.groupId());
System.out.println(mavenModuleInfo.artifactId());
System.out.println(mavenModuleInfo.version());
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void doSomethingJava9() {
Class<?> callerClass = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).getCallerClass();
MavenModuleInfo mavenModuleInfo = callerClass.getPackage().getAnnotation(MavenModuleInfo.class);
System.out.println(mavenModuleInfo.groupId());
System.out.println(mavenModuleInfo.artifactId());
System.out.println(mavenModuleInfo.version());
}
}
虽然 doSomething()
也适用于旧的 Java 版本(在 Java 8 上测试),但在 Java 9+ 上,您可以使用 doSomethingJava9()
中所示的 JEP 259 Stack-Walking API。在这种情况下,您无需手动解析异常堆栈跟踪并处理异常。
解决方案 C:通过 URL 类加载器识别调用 JAR
假设您使用我的示例项目并从应用程序模块调用库(如上一节中所述),打印 JAR 信息的一种快速而肮脏的方法是:
将此方法添加到LibraryClass
:
public void doSomethingClassLoader() {
StackTraceElement callerStackTraceElement = new Exception().getStackTrace()[1];
try {
Class<?> callerClass = Class.forName(callerStackTraceElement.getClassName());
// Cheap way of getting Maven artifact name - TODO: parse
System.out.println(
callerClass
.getClassLoader()
.getResource(callerStackTraceElement.getClassName().replaceAll("[.]","/") + ".class")
);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
同样,在 Java 9+ 上,您可以通过使用 Stack-Walking API 使代码更好,见上文。
从 Application
调用方法:
public class Application {
public static void main(String[] args) {
// new LibraryClass().doSomething();
// new LibraryClass().doSomethingJava9();
new LibraryClass().doSomethingClassLoader();
}
}
现在从命令行构建 Maven 应用程序并使用 3 个不同的类路径运行,指向
-
target/classes
目录 -
target
目录中的 JAR - 本地Maven存储库中的JAR 为了查看打印到控制台的信息类型:
$ mvn install
(...)
$ java -cp "annotation\target\annotation-1.0-SNAPSHOT.jar;library\target\library-1.0-SNAPSHOT.jar;application\target\classes" de.scrum_master.stackoverflow.q68321439.application.Application
file:/C:/Users/alexa/Documents/java-src/SO_Maven_ArtifactInfoRuntime_68321439/application/target/classes/de/scrum_master/stackoverflow/q68321439/application/Application.class
$ java -cp "annotation\target\annotation-1.0-SNAPSHOT.jar;library\target\library-1.0-SNAPSHOT.jar;application\target\application-1.0-SNAPSHOT.jar" de.scrum_master.stackoverflow.q68321439.application.Application
jar:file:/C:/Users/alexa/Documents/java-src/SO_Maven_ArtifactInfoRuntime_68321439/application/target/application-1.0-SNAPSHOT.jar!/de/scrum_master/stackoverflow/q68321439/application/Application.class
$ java -cp "annotation\target\annotation-1.0-SNAPSHOT.jar;library\target\library-1.0-SNAPSHOT.jar;c:\Users\Alexa\.m2\repository\de\scrum-master\stackoverflow\q68321439\application\1.0-SNAPSHOT\application-1.0-SNAPSHOT.jar" de.scrum_master.stackoverflow.q68321439.application.Application
jar:file:/C:/Users/alexa/.m2/repository/de/scrum-master/stackoverflow/q68321439/application/1.0-SNAPSHOT/application-1.0-SNAPSHOT.jar!/de/scrum_master/stackoverflow/q68321439/application/Application.class
如你所见
- 在第 1 种情况下,您可以从项目路径中间接推断出 Maven 工件,
- 在第 2 种情况下,您会在 JAR 名称中看到工件 ID 和版本,并在项目路径中间接看到组 ID,
- 在第 3 种情况下,您会直接在 Maven 存储库路径中看到 JAR 名称中的工件 ID 和版本以及组 ID。
当然,您可以解析该信息并以更结构化的方式打印它,但我建议简单地像这样打印它,让阅读日志的人脑进行解析。
就像我之前在评论中所说的那样,这在我向您展示的情况下非常有效,也适用于不同的项目,而不仅仅是在单个多模块项目中。在应用服务器部署或 uber JAR 情况下,您会看到哪些类型的信息,很大程度上取决于具体情况。没有单一的、通用的答案,我不能为你完成你的全部工作。我向你展示了几个选项,现在你可以选择一个。
,解决方案针对帖子主要描述中解释的场景,并考虑到每个项目根目录中存在的 pom。
注意 - 请查看 @kriegaex 建议以获得更好的方法。
-
添加maven-model依赖:
<dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-model</artifactId> <version>3.3.1</version> </dependency>
-
此依赖项具有有助于在运行时和跨项目获取所有 pom 相关信息的方法。
快速示例:
model.getArtifactId() -> 获取项目的工件 ID。
> import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
public class MavenTest {
private static String FILE_NAME = "pom.xml";
private MavenXpp3Reader reader;
private Logger log = Logger.getLogger(MavenUtil.class.getName());
private Model model;
public MavenUtil() throws FileNotFoundException,IOException,XmlPullParserException {
this(FILE_NAME);
}
public MavenUtil(String absoluteFilePath) throws FileNotFoundException,XmlPullParserException {
log.info("[" + this.getClass().getSimpleName() + "]");
reader = new MavenXpp3Reader();
FileReader fr = null;
try {
fr = new FileReader(absoluteFilePath);
model = reader.read(fr);
} finally {
if (fr != null)
fr.close();
}
}
public String[] populateBuildInfo() {
String[] buildInfo = { model.getArtifactId().toUpperCase() + ":" + model.getVersion() };
return buildInfo;
}
public String getArtifactId(String absoluteFilePath) {
return model.getArtifactId();
}
}