gpt4 book ai didi

java - 如何使用gradle为servlet创建MANIFEST.MF文件?

转载 作者:行者123 更新时间:2023-12-03 05:04:00 28 4
gpt4 key购买 nike

如何使用gradlecreate a可部署的WAR文件,尤其是正确的MANIFEST.MF文件?
生成的WAR文件:

[nsaunders@rolly libs]$ 
[nsaunders@rolly libs]$ ls
hello_jsp.war
[nsaunders@rolly libs]$
[nsaunders@rolly libs]$ jar xf hello_jsp.war
[nsaunders@rolly libs]$
[nsaunders@rolly libs]$ tree
.
├── hello_jsp.war
├── META-INF
│   └── MANIFEST.MF
└── WEB-INF
├── classes
│   └── servlets
│   └── NewServlet.class
└── lib
├── checker-qual-2.11.1.jar
├── commons-math3-3.6.1.jar
├── error_prone_annotations-2.3.4.jar
├── failureaccess-1.0.1.jar
├── guava-29.0-jre.jar
├── j2objc-annotations-1.3.jar
├── jsr305-3.0.2.jar
└── listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar

5 directories, 11 files
[nsaunders@rolly libs]$
[nsaunders@rolly libs]$ cat META-INF/MANIFEST.MF
Manifest-Version: 1.0

[nsaunders@rolly libs]$
但是,如上所述, MANIFEST.MF实际上为空白。
样本servlet:
package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class NewServlet extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet NewServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}
构建文件:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java Library project to get you started.
* For more details take a look at the Java Libraries chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.5/userguide/java_library_plugin.html
*/

plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
id 'war'
// id 'org.gretty'
}

repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}

dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'

// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:29.0-jre'

// Use TestNG framework, also requires calling test.useTestNG() below
testImplementation 'org.testng:testng:7.2.0'


providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
testCompile 'junit:junit:4.12'


// https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
// providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'




}

test {
// Use TestNG for unit tests
useTestNG()
}
可能只需要为 MANIFEST.MF文件添加一个?
项目树:
hello_jsp/
├── build.gradle
├── gradle
│   └── wrapper
│   └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── LICENSE
├── README.md
├── settings.gradle
└── src
└── main
└── java
└── servlets
└── NewServlet.java

6 directories, 8 files
对于servlet和JSP来说,这只是一个非常基本的hello-world Web应用程序。

最佳答案

https://docs.gradle.org/current/userguide/building_java_projects.html#sec:jar_manifest
也有将模板外部化的选项。
也适用于 war

jar {
manifest {
attributes("Implementation-Title": "Gradle",
"Implementation-Version": archiveVersion)
}
}

关于java - 如何使用gradle为servlet创建MANIFEST.MF文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62496188/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com