런타임에 Maven 아티팩트 버전 가져 오기
Maven 아티팩트의 JAR에서 project.version 속성이 두 파일에 포함되어 있음을 알았습니다.
META-INF/maven/${groupId}/${artifactId}/pom.properties
META-INF/maven/${groupId}/${artifactId}/pom.xml
런타임에이 버전을 읽는 권장 방법이 있습니까?
특정 라이브러리 / 클래스의 버전 정보를 얻기 위해 Maven 특정 파일에 액세스 할 필요는 없습니다.
getClass().getPackage().getImplementationVersion()
.jar-files에 저장된 버전 정보를 얻는 데 사용할 수 있습니다 MANIFEST.MF
.
운 좋게도 Maven은 충분히 영리합니다
불행하게도 Maven은 기본적으로 매니페스트에 올바른 정보를 쓰지 않습니다!
대신 하나는 수정이 <archive>
의 구성 요소 maven-jar-plugin
세트 addDefaultImplementationEntries
와 addDefaultSpecificationEntries
에 true
이 같은 :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
이상적으로이 구성은 회사 pom
나 다른베이스 폼에 배치해야합니다 .
<archive>
요소 에 대한 자세한 설명서 는 Maven Archive 설명서를 참조하십시오 .
A에 대한 위의 대답을 따르 .war
유물, 나는에 해당하는 구성을 적용했다 발견 maven-war-plugin
보다는 maven-jar-plugin
:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
이에 버전 정보를 추가 MANIFEST.MF
프로젝트의에 .jar
(포함 WEB-INF/lib
의 .war
)
다음은 pom.properties에서 버전을 가져 와서 매니페스트에서 가져 오는 방법입니다.
public synchronized String getVersion() {
String version = null;
// try to load from maven properties first
try {
Properties p = new Properties();
InputStream is = getClass().getResourceAsStream("/META-INF/maven/com.my.group/my-artefact/pom.properties");
if (is != null) {
p.load(is);
version = p.getProperty("version", "");
}
} catch (Exception e) {
// ignore
}
// fallback to using Java API
if (version == null) {
Package aPackage = getClass().getPackage();
if (aPackage != null) {
version = aPackage.getImplementationVersion();
if (version == null) {
version = aPackage.getSpecificationVersion();
}
}
}
if (version == null) {
// we could not compute the version so use a blank
version = "";
}
return version;
}
나는 여기에 두 가지 주요 접근 방식에 시간을 보냈지 만 그들은 나를 위해 운동하지 않았습니다. 빌드에 Netbeans를 사용하고 있으며 더 많은 작업이있을 수 있습니다. Maven 3의 일부 오류와 경고가 있지만 일부는 쉽게 수정할 수 있다고 생각합니다. 더 큰.
DZone 의이 기사에서 유지 관리 가능하고 구현하기 쉬운 답변을 찾았습니다.
이미 resources / config 하위 폴더를 가지고 있고 지원 URL과 같이 유지할 수있는 것들을 더 잘 반영하기 위해 app.properties라는 파일 이름을 지정했습니다.
유일한주의 사항은 Netbeans가 IDE에서 필터링을 해제해야한다는 경고를 표시한다는 것입니다. 어디서 / 어떻게 확실하지 않습니다. 이 시점에서는 효과가 없습니다. 다리를 건너야 할 경우 해결 방법이있을 수 있습니다. 행운을 빌어 요.
maven-assembly-plugin
내 메이븐 포장에 사용 하고 있습니다. Joachim Sauer의 답변 에서 Apache Maven Archiver 를 사용하면 다음과 같이 작동 할 수 있습니다.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
<executions>
<execution .../>
</executions>
</plugin>
Because archiever is one of maven shared components, it could be used by multiple maven building plugins, which could also have conflict if two or more plugins introduced, including archive
configuration inside.
To get this running in Eclipse, as well as in a Maven build, you should add the addDefaultImplementationEntries
and addDefaultSpecificationEntries
pom entries as described in other replies, then use the following code:
public synchronized static final String getVersion() {
// Try to get version number from pom.xml (available in Eclipse)
try {
String className = getClass().getName();
String classfileName = "/" + className.replace('.', '/') + ".class";
URL classfileResource = getClass().getResource(classfileName);
if (classfileResource != null) {
Path absolutePackagePath = Paths.get(classfileResource.toURI())
.getParent();
int packagePathSegments = className.length()
- className.replace(".", "").length();
// Remove package segments from path, plus two more levels
// for "target/classes", which is the standard location for
// classes in Eclipse.
Path path = absolutePackagePath;
for (int i = 0, segmentsToRemove = packagePathSegments + 2;
i < segmentsToRemove; i++) {
path = path.getParent();
}
Path pom = path.resolve("pom.xml");
try (InputStream is = Files.newInputStream(pom)) {
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(is);
doc.getDocumentElement().normalize();
String version = (String) XPathFactory.newInstance()
.newXPath().compile("/project/version")
.evaluate(doc, XPathConstants.STRING);
if (version != null) {
version = version.trim();
if (!version.isEmpty()) {
return version;
}
}
}
}
} catch (Exception e) {
// Ignore
}
// Try to get version number from maven properties in jar's META-INF
try (InputStream is = getClass()
.getResourceAsStream("/META-INF/maven/" + MAVEN_PACKAGE + "/"
+ MAVEN_ARTIFACT + "/pom.properties")) {
if (is != null) {
Properties p = new Properties();
p.load(is);
String version = p.getProperty("version", "").trim();
if (!version.isEmpty()) {
return version;
}
}
} catch (Exception e) {
// Ignore
}
// Fallback to using Java API to get version from MANIFEST.MF
String version = null;
Package pkg = getClass().getPackage();
if (pkg != null) {
version = pkg.getImplementationVersion();
if (version == null) {
version = pkg.getSpecificationVersion();
}
}
version = version == null ? "" : version.trim();
return version.isEmpty() ? "unknown" : version;
}
If your Java build puts target classes somewhere other than "target/classes", then you may need to adjust the value of segmentsToRemove.
On my spring boot application, the solution from the accepted answer worked until I recently updated my jdk to version 12. Tried all the other answers as well and couldn't get that to work.
At that point, I added the below line to the first class of my spring boot application, just after the annotation @SpringBootApplication
@PropertySources({
@PropertySource("/META-INF/maven/com.my.group/my-artefact/pom.properties")
})
Later I use the below to get the value from the properties file in whichever class I want to use its value and appVersion
gets the project version to me:
@Value("${version}")
private String appVersion;
Hope that helps someone.
Java 8 variant for EJB in war file with maven project. Tested on EAP 7.0.
@Log4j // lombok annotation
@Startup
@Singleton
public class ApplicationLogic {
public static final String DEVELOPMENT_APPLICATION_NAME = "application";
public static final String DEVELOPMENT_GROUP_NAME = "com.group";
private static final String POM_PROPERTIES_LOCATION = "/META-INF/maven/" + DEVELOPMENT_GROUP_NAME + "/" + DEVELOPMENT_APPLICATION_NAME + "/pom.properties";
// In case no pom.properties file was generated or wrong location is configured, no pom.properties loading is done; otherwise VERSION will be assigned later
public static String VERSION = "No pom.properties file present in folder " + POM_PROPERTIES_LOCATION;
private static final String VERSION_ERROR = "Version could not be determinated";
{
Optional.ofNullable(getClass().getResourceAsStream(POM_PROPERTIES_LOCATION)).ifPresent(p -> {
Properties properties = new Properties();
try {
properties.load(p);
VERSION = properties.getProperty("version", VERSION_ERROR);
} catch (Exception e) {
VERSION = VERSION_ERROR;
log.fatal("Unexpected error occured during loading process of pom.properties file in META-INF folder!");
}
});
}
}
참고URL : https://stackoverflow.com/questions/2712970/get-maven-artifact-version-at-runtime
'IT' 카테고리의 다른 글
날짜 시간 소인을 작성하고 ISO 8601, RFC 3339, UTC 시간대로 형식을 지정하는 방법은 무엇입니까? (0) | 2020.05.29 |
---|---|
컴퓨터에서 .NET Framework 버전을 반환하는 PowerShell 스크립트? (0) | 2020.05.29 |
'SubSonic.Schema .DatabaseColumn'유형의 오브젝트를 직렬화하는 중에 순환 참조가 발견되었습니다. (0) | 2020.05.28 |
string.isEmpty () 또는“”.equals (string)을 사용해야합니까? (0) | 2020.05.28 |
경로에서 폴더 이름 얻기 (0) | 2020.05.28 |