方案一:Maven 资源过滤
你的 POM sn-p 应该正确地替换变量,如果你把它放在正确的 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。在这里你可以看到我们如何在构建过程中通过 Templating Maven Plugin 使用 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。在这里你可以看到我们如何在构建过程中通过 Templating Maven Plugin 使用 Maven 属性被其对应的值替换:
/**
* This is the package description (...)
*/
@MavenModuleInfo(groupId = "${project.groupId}", artifactId = "${project.artifactId}", 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+ 上,您可以使用JEP 259 Stack-Walking API,如doSomethingJava9() 所示。在这种情况下,您无需手动解析异常堆栈跟踪和处理异常。
解决方案 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 中,您会在 JAR 名称中看到工件 ID 和版本,并直接在 Maven 存储库路径中看到组 ID。
当然,您可以解析这些信息并以更有条理的方式打印出来,但我建议像这样简单地打印它,让读取日志的人脑进行解析。
就像我之前在评论中所说的那样,这在我向您展示的情况下效果很好,也适用于不同的项目,而不仅仅是在单个多模块项目中。在应用程序服务器部署或 uber JAR 情况下,您会看到什么样的信息,很大程度上取决于具体情况。没有单一的通用答案,我不能为你做你的全部工作。我向你展示了几个选项,现在你可以选择一个。