maven, jsp files in dependency

橙三吉。 提交于 2019-12-05 17:41:43

问题


I'm using maven2 for dependency management. I have one project that contains some Java files and some jsp files and another project, a web project, that depends on the first project. How do I access the jsp files from the web project?

I can see that the jsp files are added to 1-0-SNAPSHOT-sources.jar and not 1-0-SNAPSHOT.jar (which is added as a dependency in the web projects pom.xml).


回答1:


I think the correct Maven-way to do it would be to put the JSP files in your web project under /src/main/webapp. If that for some reason is not possible, you could use the Maven Dependency Plugin to copy the needed files into your webapp. Or, if you have a WAR project anyway, you could use an Overlay to copy the JSP-Files. The second option (which I'd recommend), would look something like this:

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <configuration>
            <overlays>
              <overlay>
                <groupId>myGroupId</groupId>
                <artifactId>myArtifactId</artifactId>
                <type>jar</type>
                <includes>
                  <include>**/*.jsp</include>
                </includes>
                <targetPath>WEB-INF/pages</targetPath>
              </overlay>
            </overlays>
          </configuration>
        </plugin>
      </plugins>
    </build>



回答2:


The problem with that solution is that when developping with Eclipse, the project doesn't handle the overlay. So, the jsp are not accessible.




回答3:


I wanted some files from a dependency JAR project into my WEB project.

I've done this way so I could have the files not only when packaging the WAR but also when running the maven servlet container plugin (i.e. jetty:run or tomcat:run).

So here's what worked for me:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.1</version>
    <executions>
        <execution>
            <id>copy-files-to-webapp-directory</id>
            <phase>compile</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>com.my.project</groupId>
                        <artifactId>my-amazing-project</artifactId>
                        <type>jar</type>
                        <overWrite>true</overWrite>
                        <outputDirectory>src/main/webapp</outputDirectory>
                        <includes>**/*.jsp, **/*.css, **/*.png</includes>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

Hope that helps anyone looking for a similar solution



来源:https://stackoverflow.com/questions/776874/maven-jsp-files-in-dependency

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!