我正在尝试从文件夹中读取文件,这些文件不在jar中,而是相对于运行jar的文件。胖罐使用maven shade plugin构建
阴影插件
<!--Start of comments-->
<div class="col-md-12">
<%= form_for [@article, @comment],
:html => {class: "form-horizontal", role: "form"} do
|f| %>
<% if @comment.errors.any? %>
<div class="panel panel-danger col-md-offset-1">
<div class="panel-heading">
<h2 class="panel-title">
<%= pluralize(@comment.error.count, "error") %>
prohibited this comment from being saved:
</h2>
<div class="panel-body">
<ul>
<% @comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
</div>
<% end %>
<div class="form-group">
<div class="control-label col-md-2">
<%= f.label :body, 'New Comment' %>
</div>
<div class="col-md-10">
<%= f.text_area :body, rows: 10, class: "form-control", placeholder: "New Comment" %>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<%= f.submit "Add Comment", class: "btn btn-primary btn-lg pull-right" %>
</div>
</div>
<% end %>
</div>
这会生成清单文件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.xx.validaor.Validator</Main-Class>
<Class-Path>csv_files2</Class-Path>
<X-Compile-Source-JDK>1.9</X-Compile-Source-JDK>
<X-Compile-Target-JDK>1.9</X-Compile-Target-JDK>
</manifestEntries>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
这是从文件夹
运行Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.3.9
Build-Jdk: 9.0.1
Class-Path: csv_files2
Main-Class: com.xx.validaor.Validator
X-Compile-Source-JDK: 1.9
X-Compile-Target-JDK: 1.9
在csv_files2里面我试图把另一个csv_files2文件夹放在一堆没有改变任何东西的文件中
java -jar my-0.0.1-SNAPSHOT.jar
logs/
csv_files2/
murex-validator-0.0.1-SNAPSHOT.jar
输出
URL resource = getClass().getClassLoader().getResource("/csv_files2/");
if (resource == null) {
logger.error("1 /csv_files2/ did not work");
}
resource = getClass().getClassLoader().getResource("csv_files2/");
if (resource == null) {
logger.error("2 csv_files2/ did not work");
}
resource = getClass().getClassLoader().getResource("/csv_files2");
if (resource == null) {
logger.error("3 /csv_files2 did not work");
}
resource = getClass().getClassLoader().getResource("csv_files2");
if (resource == null) {
logger.error("4 csv_files2 did not work");
}
resource = getClass().getClassLoader().getResource("./csv_files2");
if (resource == null) {
logger.error("5 ./csv_files2 did not work");
}
File[] files2 = Paths.get(resource.toURI()).toFile().listFiles();
答案 0 :(得分:0)
尝试在这样的阴影插件中添加'/'到csv_files2的末尾
<Main-Class>com.td.validaor.Validator</Main-Class>
<Class-Path>csv_files2/</Class-Path>
<X-Compile-Source-JDK>1.9</X-Compile-Source-JDK>
<X-Compile-Target-JDK>1.9</X-Compile-Target-JDK>