2015-06-16 92 views
0

我試圖用gradle構建我的項目,但由於某種原因資源被放在與其實際級別不同的級別上。 這裏的身材:Gradle Jar結構與實際項目結構不同

apply plugin: 'java' 
version = '1.1' 
archivesBaseName = 'DesktopOmegle' 
repositories { 
//mavenCentral() 
maven { 
    url 'http://oss.sonatype.org/content/repositories/snapshots/' 
    url "http://repo1.maven.org/maven2" 
} 
} 
dependencies { 
compile 'org.json:json:20090211' 
compile 'org.fxmisc.richtext:richtextfx:0.6.4' 
} 
jar { 
    from { 
     configurations.compile.collect { 
     it.isDirectory() ? it : zipTree(it) 
     } 
    } 

    manifest { 
     attributes 'Main-Class': 'main.java.client.OmegleClient' 
    } 
} 
task copyDeps(type: Copy) { 
    from configurations.runtime 
    into 'lib' 
    rename { 
      String fileName -> 
      fileName.replace('-20090211', '') 
    } 
} 
build.dependsOn(copyDeps) 

,這裏是該項目的實際結構(只顯示src文件夾):

|src- 
| main- 
|  java- 
|    client-... 
|    server-... 
|  resources- 
|     images-... 

產生的罐子結構如下:

|jar- 
| images-... 
| main- 
|  java-   
|    client-... 
|    server-... 
| META_INF-... 
| org-... 

顯然那不是我想要的。我希望jar中的圖像位於main.resources內部,因爲它們是實際的。爲什麼jar沒有堅持項目的真正結構?有人能幫我糾正這種情況嗎?

編輯: 代碼段用於訪問資源:

ClientConstants.RESOURCES = "/main/resources/images/"; 
if (status.equals(ClientConstants.STATUS_OFFLINE)) 
{ 
    img = ClientConstants.RESOURCES+"ON.png"; 
    action = "Connect"; 
} 
else 
{ 
    img = ClientConstants.RESOURCES+"OFF.png"; 
    action = "Disconnect"; 
} 
Image imageConnect = new Image(getClass().getResourceAsStream(img)); 
ImageView viewBottom = new ImageView(imageConnect); 
+0

這個結構是正確的。什麼是真正的問題? – Opal

+0

images文件夾不在我想要的位置。我希望jar結構與項目結構相同。這意味着圖像應該在main.resources中。那可能嗎? – maxam

+0

可能是,但不符合標準。 – Opal

回答

1

當您的項目由gradle這個管理你應該總是使用介紹標準佈局的行家或有一個很好的藉口,使用任何不同的佈局。 Here您可以找到示例項目,其中顯示瞭如何使用此佈局並閱讀稍後將包含在jar中的資源。這些資源將始終置於jar文件的根目錄下。而下面你可以找到示例代碼,演示瞭如何讀取這些文件:從here採取

private void readResource(String path) { 
     InputStream in = getClass().getClassLoader().getResourceAsStream(path); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 

     try (Scanner scanner = new Scanner(reader)) { 

      while (scanner.hasNextLine()) { 
       String line = scanner.nextLine(); 
       System.out.println("LINE: " + line); 
      } 

      scanner.close(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

示例代碼。