jamesladd / stc

stc
Other
3 stars 2 forks source link

Run example using Maven, no IDE required #4

Closed mattys101 closed 6 years ago

mattys101 commented 6 years ago

I noticed you mentioned that the current stuff can only be run using the IDE. You could set up an execution target in Maven so that you can run it easily without the IDE. If you want to that is. The code I use is below ;-)

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <configuration>
        <executable>java></executable>
        <mainClass>st.redline.Stic</mainClass>
        <arguments>
            <argument>st.redline.example.Test</argument>
        </arguments>
    </configuration>
</plugin>

The only problem with this, is you need to make your classpath handling more robust since Maven modifies the classpath at runtime, rather than specifying java.class.path variable.

Here's my hacky change to Stic's classPaths() method:

public String[] classPaths() {
    String[] classPaths = classPath().split(File.pathSeparator);

    ArrayList<String> urlClassPaths = new ArrayList<>(java.util.Arrays.asList(classPaths));
    URLClassLoader cl = (URLClassLoader) (Thread.currentThread().getContextClassLoader());
    for (URL url : cl.getURLs()) {
        if ("file".equals(url.getProtocol())) {
            urlClassPaths.add(url.getFile());
        }
    }

    classPaths = urlClassPaths.toArray(classPaths);

    return classPaths;
}

Cheers,

Matt

jamesladd commented 6 years ago

I'll make it work :)

Sent from my Commodore 64

On 26 Sep 2017, at 8:17 pm, Matt notifications@github.com wrote:

I noticed you mentioned that the current stuff can only be run using the IDE. You could set up an execution target in Maven so that you can run it easily without the IDE. If you want to that is. The code I use is below ;-)

org.codehaus.mojo exec-maven-plugin 1.6.0 java> st.redline.Stic st.redline.example.Test

The only problem with this, is you need to make your classpath handling more robust since Maven modifies the classpath at runtime, rather than specifying java.class.path variable.

Here's my hacky change to Stic's classPaths() method:

public String[] classPaths() { String[] classPaths = classPath().split(File.pathSeparator);

ArrayList<String> urlClassPaths = new ArrayList<>(java.util.Arrays.asList(classPaths));
URLClassLoader cl = (URLClassLoader) (Thread.currentThread().getContextClassLoader());
for (URL url : cl.getURLs()) {
    if ("file".equals(url.getProtocol())) {
        urlClassPaths.add(url.getFile());
    }
}

classPaths = urlClassPaths.toArray(classPaths);

return classPaths;

} Cheers,

Matt

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

jamesladd commented 6 years ago

branch: jcl/classpath

Redline must find a Smalltalk file in the classpath or in a JAR. This also enables Redline to be run from the command line.

You can run Redline from the command line with the following command to run Test.st

java -cp redline-0.6-SNAPSHOT.jar -jar redline-0.6-SNAPSHOT.jar st.redline.example.Test

NOTE

When compiling with Maven (mvn clean package) the resulting JAR is in the target folder, including the Smalltalk sources so the following shorter command line also works.

java -jar target/redline-0.6-SNAPSHOT.jar st.redline.example.Test