Last active
March 12, 2019 19:31
-
-
Save jeffque/b26f8044a3b7b1177b19182b05e96482 to your computer and use it in GitHub Desktop.
Criando um `.jar` copiado e compilando-o com TC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.softsite.streamsupport; | |
... | |
import java.io.Closeable; | |
import java.io.IOException; | |
import java.util.function.Supplier; | |
import com.totalcross.util.compile.CompilationBuilder; | |
import totalcross.io.File; | |
import totalcross.ui.MainWindow; | |
import totalcross.util.IOUtils; | |
import totalcross.util.concurrent.Lock; | |
... | |
public class CompileApp { | |
... | |
public void geraBuild() throws IOException, InterruptedException { | |
String tcKey = <My Key>; | |
String tcHome = <TC Home>; | |
Class<? extends MainWindow> mainWindowClass = App.class; | |
try (Closeable c = createCopiedJar(mainWindowClass)) { | |
new CompilationBuilder() | |
.setKey(tcKey) | |
.setTotalCrossHome(tcHome) | |
.setPlatformsTarget(AvailablePlatforms.WIN32) | |
.setMustCompile(s -> s.contains("totalcross-functional-toolbox")) | |
.setMainTarget(App.class) | |
.singlePackage() | |
.build(); | |
} | |
} | |
private Closeable createCopiedJar(Class<? extends MainWindow> mainWindowClass) throws IOException { | |
String origName = s.get(); | |
String destinyName = getNameMainWindow(mainWindowClass); | |
if (origName.equals(destinyName)) { | |
return () -> {}; | |
} | |
try { | |
try ( | |
File orig = new File(s.get(), File.READ_ONLY); | |
File destiny = new File(destinyName, File.CREATE_EMPTY); | |
){ | |
IOUtils.copy(orig.asInputStream(), destiny.asOutputStream(), 2048); | |
} catch (totalcross.io.IOException e) { | |
try (File f = new File(destinyName)) { | |
f.delete(); | |
} | |
throw new IOException(e); | |
} catch (IOException e) { | |
try (File f = new File(destinyName)) { | |
f.delete(); | |
} | |
throw e; | |
} | |
} catch (totalcross.io.IOException e) { | |
throw new IOException(e); | |
} | |
return () -> { | |
try (File f = new File(destinyName)) { | |
f.delete(); | |
} catch (totalcross.io.IOException e) { | |
throw new IOException(e); | |
} | |
}; | |
} | |
private String cleanedName(Class<? extends MainWindow> mainWindowClass) { | |
return mainWindowClass.getName().replaceAll("^.*\\.", ""); | |
} | |
private String getNameMainWindow(Class<? extends MainWindow> mainWindowClass) { | |
return "target/" + cleanedName(mainWindowClass) + ".jar"; | |
} | |
private Lock lock = new Lock(); | |
private String jarPath = null; | |
private Supplier<String> s = () -> { | |
synchronized (lock) { | |
if (jarPath == null) { | |
jarPath = "target/<maven-generated-name>.jar"; // se desejar, pode derivar esse valor programaticamente também | |
s = () -> jarPath; | |
} | |
return jarPath; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment