在Liferay 7 CE中将非OSGI jar依赖项添加到基于maven的MVC portlet中

时间:2017-01-09 07:38:21

标签: maven-2 liferay osgi osgi-bundle liferay-7

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

项目结构:

enter image description here

的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: *

的build.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"
        }
    }

SecondMavenPortletPortlet.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);
        }
    }

3 个答案:

答案 0 :(得分:2)

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

看看这些选项是否符合您的要求。您应该选择此博客中提到的第3或第4选项。它在我的案例中起作用。

答案 1 :(得分:1)

我们也在我们的项目中遇到过类似的问题。我注意到最后一个答案是从1月15日开始,并认为在引用的Liferay博客条目中包含有用的回复可能会很好。这使我们意识到我们必须从博客中选择选项3或4,如前面的答案所述。

  

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

     

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

     

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

     

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

     

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

答案 2 :(得分:0)

如果com.vividsolutions.jts没有准备好OSGi,你可以通过bnd或SpringRoo

包装它