1

我已经为 Liferay 7 CE 创建了一个具有相应 maven 原型的 MVC Portlet(如此所述)。我在项目的 POM 文件中添加了一些额外的非 OSGI jar 文件作为项目依赖项。当我在 liferay 门户服务器上部署项目时,OSGI 容器无法解析创建模块的依赖关系,并且捆绑包仍处于安装阶段。我想以自动方式将非 OSGI jar 文件及其传递依赖项添加到捆绑包中。我应该怎么办?模块内容如下:

项目结构:

在此处输入图像描述

pom.xml:

    <?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>org.ranginkaman</groupId>
        <artifactId>second-maven-portlet</artifactId>
        <version>1.0.0</version>
        <packaging>jar</packaging>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
        <dependencies>
            <dependency>
                <groupId>com.liferay.portal</groupId>
                <artifactId>com.liferay.portal.kernel</artifactId>
                <version>2.0.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.liferay.portal</groupId>
                <artifactId>com.liferay.util.taglib</artifactId>
                <version>2.0.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.portlet</groupId>
                <artifactId>portlet-api</artifactId>
                <version>2.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.0.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.osgi</groupId>
                <artifactId>org.osgi.compendium</artifactId>
                <version>5.0.0</version>
                <scope>provided</scope>
            </dependency>

            <!-- https://mvnrepository.com/artifact/com.vividsolutions/jts -->
            <dependency>
                <groupId>com.vividsolutions</groupId>
                <artifactId>jts</artifactId>
                <version>1.13</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <archive>
                            <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                        </archive>
                        <excludes>
                            <exclude>**/META-INF/resources/**/.sass-cache/</exclude>
                        </excludes>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>biz.aQute.bnd</groupId>
                    <artifactId>bnd-maven-plugin</artifactId>
                    <version>3.2.0</version>
                    <executions>
                        <execution>
                            <id>default-bnd-process</id>
                            <goals>
                                <goal>bnd-process</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>biz.aQute.bnd</groupId>
                            <artifactId>biz.aQute.bndlib</artifactId>
                            <version>3.2.0</version>
                        </dependency>
                        <dependency>
                            <groupId>com.liferay</groupId>
                            <artifactId>com.liferay.ant.bnd</artifactId>
                            <version>2.0.28</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <groupId>com.liferay</groupId>
                    <artifactId>com.liferay.css.builder</artifactId>
                    <version>1.0.20</version>
                    <executions>
                        <execution>
                            <id>default-build-css</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>build-css</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <portalCommonPath>/</portalCommonPath>
                        <docrootDirName>src/main/resources</docrootDirName>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

bnd.bnd:

    Bundle-Name: second-maven-portlet
    Bundle-SymbolicName: org.ranginkaman
    Bundle-Version: 1.0.0
    -jsp: *.jsp,*.jspf
    -plugin.jsp: com.liferay.ant.bnd.jsp.JspAnalyzerPlugin
    -plugin.resourcebundle: com.liferay.ant.bnd.resource.bundle.ResourceBundleLoaderAnalyzerPlugin
    -plugin.sass: com.liferay.ant.bnd.sass.SassAnalyzerPlugin
    -sass: *

构建.gradle:

    buildscript {
        dependencies {
            classpath group: "com.liferay", name: "com.liferay.gradle.plugins", version: "3.0.23"
        }

        repositories {
            mavenLocal()

            maven {
                url "https://cdn.lfrs.sl/repository.liferay.com/nexus/content/groups/public"
            }
        }
    }

    apply plugin: "com.liferay.plugin"

    dependencies {
        compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
        compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0"
        compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
        compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
        compileOnly group: "jstl", name: "jstl", version: "1.2"
        compileOnly group: "org.osgi", name: "org.osgi.compendium", version: "5.0.0"
    }

    repositories {
        mavenLocal()

        maven {
            url "https://cdn.lfrs.sl/repository.liferay.com/nexus/content/groups/public"
        }
    }

第二个MavenPortletPortlet.java:

    package org.ranginkaman.portlet;

    import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
    import com.vividsolutions.jts.geom.Coordinate;
    import com.vividsolutions.jts.geom.GeometryFactory;
    import com.vividsolutions.jts.geom.Point;

    import java.io.IOException;

    import javax.portlet.Portlet;
    import javax.portlet.PortletException;
    import javax.portlet.RenderRequest;
    import javax.portlet.RenderResponse;

    import org.osgi.service.component.annotations.Component;

    @Component(
        immediate = true,
        property = {
            "com.liferay.portlet.display-category=category.sample",
            "com.liferay.portlet.instanceable=true",
            "javax.portlet.display-name=second-maven-portlet Portlet",
            "javax.portlet.init-param.template-path=/",
            "javax.portlet.init-param.view-template=/view.jsp",
            "javax.portlet.resource-bundle=content.Language",
            "javax.portlet.security-role-ref=power-user,user"
        },
        service = Portlet.class
    )
    public class SecondMavenPortletPortlet extends MVCPortlet {
        @Override
        public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
                throws IOException, PortletException {


            GeometryFactory geometryFactory = new GeometryFactory();

            Point point = geometryFactory.createPoint(new Coordinate(1.0, 2.0));

            System.out.println(point);

            super.doView(renderRequest, renderResponse);
        }
    }
4

3 回答 3

2

这是一个 Liferay 社区博客,用于添加对 OSGi 模块的依赖项: https ://web.liferay.com/web/user.26526/blog/-/blogs/osgi-module-dependencies

看看这些选项是否符合您的要求。您应该选择本博客中提到的第 3 或第 4 选项。它在我的情况下有效。

于 2017-01-15T20:18:06.627 回答
1

在我们的一个项目中,我们也遇到了类似的问题。我注意到最后一个答案是从 1 月 15 日开始的,我认为最好对已引用的 Liferay 博客条目进行有用的回复。这就是让我们意识到我们必须从博客中选择选项 3 或 4 的原因,如上一个答案中所述。

所以这实际上是一个常见的误解——使用依赖管理器在 gradle 中构建和没有类似依赖管理器的 OSGi 运行时环境之间存在差异。

OSGi 故意不下载依赖项,尤其是传递依赖项,因为这会导致环境不稳定。

编译代码时,构建过程将包括传递依赖关系,但 BND 不是编译器,它只是构建 jar 文件并包括你告诉它的内容。

所以是的,在正常情况下,您必须自己包含所有依赖项和传递依赖项。也就是说,一些项目在其传递依赖项中不使用“可选”标记,因此 OSGi 将它们视为必备项。您必须在 BND 文件中添加类路径声明以强制排除那些您真的不想要的包。

这绝对是一个 PITA,但它是有原因的。OSGi 环境是您的运行时环境,它是您想要主动管理和了解的环境。作为管理员,您想知道您的模块正在使用什么并适当地管理部署。从管理的角度来看,您不希望环境只是自己下载东西(而且通常这在生产环境中是不可能的)。

于 2017-04-21T12:32:48.813 回答
0

如果 com.vividsolutions.jts 未准备好 OSGi,您可以通过bnd或 SpringRoo将其包装

于 2017-01-12T16:07:35.007 回答