【问题标题】:How to I get sublibrary manifests from uber jar如何从 uber jar 获取子库清单
【发布时间】:2016-03-29 05:04:24
【问题描述】:

我正在构建一个 uber jar,并试图提取子库清单文件的版本信息。由于某种原因,清单信息不再出现在 jar 中。

这曾经是一个使用 war 插件构建的项目,并且有效,但是,我无法弄清楚如何使用 jar 插件获得相同的行为。

这是我目前的配置。我不确定为什么它不再起作用。

我怎样才能提取图书馆清单信息,或者是什么导致这不起作用?

可执行的 pom 文件

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>package-jar-with-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>com.clearwateranalytics.perseus.worker.PerseusWorker</mainClass>
                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                            </manifest>
                            <manifestEntries>
                                <Implementation-Build>${build.number}</Implementation-Build>
                                <Build-Date>${maven.build.timestamp}</Build-Date>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

库 pom

<build>
    <resources>
        <resource>
            <directory>${project.basedir}/src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Implementation-Build>${build.number}</Implementation-Build>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

获取构建信息的库方法

private static String getBuildNumber()
{
    try
    {
        Enumeration<URL> resources = ServerVersionDao.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements())
        {
            try
            {
                Manifest manifest = new Manifest(resources.nextElement().openStream());
                Attributes attributes = manifest.getMainAttributes();
                if ("perseus-server-lib".equals(attributes.getValue("Implementation-Title")))
                {
                    String versionNumber = getVersion(ServerVersionDao.class);
                    String buildNumber = attributes.getValue("Implementation-Build");
                    String build = "";
                    if (versionNumber != null)
                        build += versionNumber;
                    if (buildNumber != null)
                        build += " b" + buildNumber;

                    if (!build.trim().isEmpty())
                        return build;
                }
            }
            catch (IOException ex)
            {
            }
        }
    }
    catch (Exception ex)
    {
    }
    throw new IllegalStateException("Got here!!!!");
}

【问题讨论】:

    标签: java maven jar pom.xml


    【解决方案1】:

    您好,我认为您不能按预期添加单个清单。这些类只是包含在 uber-jar 中,不是作为 jar 而是作为包。

    但是,如果您想检索作为依赖项之一的特定库的版本(对吗?),您最终可以遵循与上述代码类似的模式,而不是尝试加载生成的 META-INF /MANIFEST,您可以在附近找到包含的依赖项的版本控制信息;)

    META-INF\maven\group-id/artifact\pom.properties
    

    例如

    META-INF\maven\perseus/perseus-serverlib\pom.properties
    

    试一试:)

    您可以在其中找到如下属性文件:

    #Generated by Maven
    #Sun Aug 02 13:18:09 PDT 2015
    version=1.8.3
    groupId=org.jsoup
    artifactId=jsoup
    

    【讨论】:

      猜你喜欢
      • 2011-01-14
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 2017-09-15
      • 2011-03-20
      • 2021-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多